substringAfterLast

Return the part of a string after the last occurrence of a specified substring.

Usage

var substringAfterLast = require( '@stdlib/string/substring-after-last' );

substringAfterLast( str, search[, fromIndex] )

Returns the part of a string after the last occurrence of a specified substring.

var str = 'beep boop';
var out = substringAfterLast( str, 'b' );
// returns 'oop'

out = substringAfterLast( str, 'o' );
// returns 'p'

By default, the search starts at the end of the string and proceeds backwards to the beginning. To start the search at a specified index, specify an integer for the fromIndex argument.

var str = 'beep boop';
var out = substringAfterLast( str, 'b', 3 );
// returns 'eep boop'

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 an empty string.

Examples

var substringAfterLast = require( '@stdlib/string/substring-after-last' );

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

out = substringAfterLast( str, 'to be' );
// returns ', that is the question.'

out = substringAfterLast( str, 'question.' );
// returns ''

out = substringAfterLast( str, 'xyz' );
// returns ''

out = substringAfterLast( str, '' );
// returns ''

CLI

Usage

Usage: substring-after-last [options] --search=<string> [<string>]

Options:

  -h,    --help                Print this message.
  -V,    --version             Print the package version.
         --search string       Search string.
         --from-index int      Index at which to start the search.
         --split sep           Delimiter for stdin data. Default: '/\\r?\\n/'.

Notes

  • If the split separator is a regular expression, ensure that the split option is either properly escaped or enclosed in quotes.

    # Not escaped...
    $ echo -n $'foo\nbar\nbaz' | substring-after-last --search a --split /\r?\n/
    
    # Escaped...
    $ echo -n $'foo\nbar\nbaz' | substring-after-last --search a --split /\\r?\\n/
    
  • The implementation ignores trailing delimiters.

Examples

$ substring-after-last abcdefg --search d
efg

To use as a standard stream,

$ echo -n $'bar\nbaz' | substring-after-last --search b
ar
az

By default, when used as a standard stream, the implementation assumes newline-delimited data. To specify an alternative delimiter, set the split option.

$ echo -n 'bar\tbaz' | substring-after-last --search b --split '\t'
ar
az
Did you find this page helpful?