Removes the first occurrence of a substring from a string.
trim = true
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' } Copy
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' }
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') } Copy
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') }
The substring to remove.
Optional. Whether to trim the resulting string. Defaults to true.
A Result object with either the resulting string on success, or an Error object if the provided input is not a string.
Result
Error
Generated using TypeDoc v0.24.7
Removes the first occurrence of a substring from a string.
Default Value
trim = true
Example
Example
Handle an invalid input string: