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 offromIndex
. - 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