Skip to content

Latest commit

 

History

History
139 lines (85 loc) · 3.54 KB

File metadata and controls

139 lines (85 loc) · 3.54 KB

replaceAfter

Replace the substring after the first occurrence of a specified search string.

Usage

var replaceAfter = require( '@stdlib/string/base/replace-after' );

replaceAfter( str, replacement, search[, fromIndex] )

Replaces the substring after the first occurrence of a specified search string.

var str = 'beep boop';
var replacement = 'foo';
var out = replaceAfter( str, replacement, 'o' );
// returns 'beep bofoo'

out = replaceAfter( str, replacement, ' ' );
// returns 'beep foo'

By default, the search starts at the beginning of the string. To start searching from a different index, provide a fromIndex argument:

var str = 'boop baz boop';
var replacement = 'foo';
var out = replaceAfter( str, replacement, 'o', 3 );
// returns 'boop baz bofoo'

Notes

  • If a substring is not present in a provided string, the function returns an empty string.
  • If provided an empty substring, the function returns the input string.
  • If fromIndex is less than 0 or greater than str.length, the search starts at index 0 and str.length, respectively.

Examples

var replaceAfter = require( '@stdlib/string/base/replace-after' );

var str = 'To be, or not to be, that is the question.';
var replacement = 'foo';
var out = replaceAfter( str, replacement, ', ' );
// returns 'To be, foo'

out = replaceAfter( str, replacement, 'to be' );
// returns 'To be, or not to befoo'

out = replaceAfter( str, replacement, 'question.' );
// returns 'To be, or not to be, that is the question.foo'

out = replaceAfter( str, replacement, 'xyz' );
// returns 'To be, or not to be, that is the question.'

out = replaceAfter( str, replacement, '' );
// returns 'foo'