Function removeFirstOccurrence

  • Removes the first occurrence of a substring from a string.

    Default Value

    trim = true

    Example

    removeFirstOccurrence('Hello World', 'lo')
    // => { ok: true, value: 'Hel World' }

    removeFirstOccurrence('Hello World', 'lo', false)
    // => { ok: true, value: 'Hel World' }

    removeFirstOccurrence(' Foo Bar ', 'Bar')
    // => { ok: true, value: ' Foo ' }

    removeFirstOccurrence('Hello World', 'xyz')
    // => { ok: true, value: 'Hello World' }

    Example

    Handle an invalid input string:

    removeFirstOccurrence(null, 'lo')
    // => { ok: false, error: Error('[strings.removeFirstOccurrence] Expected string value as input') }

    removeFirstOccurrence('Hello World', null)
    // => { ok: false, error: Error('[strings.removeFirstOccurrence] Expected string value for the searchstr parameter') }

    Parameters

    • str: string
    • searchstr: string

      The substring to remove.

    • trim: boolean = true

      Optional. Whether to trim the resulting string. Defaults to true.

    Returns Result<string, Error>

    A Result object with either the resulting string on success, or an Error object if the provided input is not a string.

Generated using TypeDoc v0.24.7