nextCodePointIndex

Return the position of the next Unicode code point in a string after a specified position.

Usage

var nextCodePointIndex = require( '@stdlib/string/next-code-point-index' );

nextCodePointIndex( string[, fromIndex] )

Returns the position of the next Unicode code point in a string after a specified position.

var out = nextCodePointIndex( 'last man standing' );
// returns 1

By default, the function searches for a Unicode code point starting from the first index. To specify an alternative starting search index, provide a fromIndex argument.

var out = nextCodePointIndex( 'last man standing', 4 );
// returns 5

Notes

  • If string is an empty string, the function returns -1 irrespective of fromIndex.
  • If a code point does not exist after fromIndex, the function returns -1.
  • Note that fromIndex does not refer to a visual character position, but to an index in the ordered sequence of UTF-16 code units.

Examples

var nextCodePointIndex = require( '@stdlib/string/next-code-point-index' );

var out = nextCodePointIndex( 'last man standing', 4 );
// returns 5

out = nextCodePointIndex( 'presidential election', 8 );
// returns 9

out = nextCodePointIndex( '๐’ป๐“Ÿ๐’ป๐“Ÿ', 0 );
// returns 2

out = nextCodePointIndex( '๐ŸŒท', 0 );
// returns -1

CLI

Usage

Usage: next-code-point-index [options] [<string>]

Options:

  -h,    --help                Print this message.
  -V,    --version             Print the package version.
         --from index          Starting search position in string. Default: 0.

Examples

$ next-code-point-index --from=0 ๐’ป๐“Ÿ๐’ป๐“Ÿ
2

To use as a standard stream,

$ echo -n '๐’ป๐“Ÿ๐’ป๐“Ÿ' | next-code-point-index --from=0
2
Did you find this page helpful?