Skip to main content
Module

x/denops_std/function/mod.ts>charidx

📚 Standard module for denops.vim
Go to Latest
function charidx
import { charidx } from "https://deno.land/x/denops_std@v6.4.0/function/mod.ts";

Return the character index of the byte at {idx} in {string}. The index of the first character is zero. If there are no multibyte characters the returned value is equal to {idx}.

When {countcc} is omitted or FALSE, then composing characters are not counted separately, their byte length is added to the preceding base character. When {countcc} is TRUE, then composing characters are counted as separate characters.

When {utf16} is present and TRUE, {idx} is used as the UTF-16 index in the String {expr} instead of as the byte index.

Returns -1 if the arguments are invalid or if there are less than {idx} bytes. If there are exactly {idx} bytes the length of the string in characters is returned.

An error is given and -1 is returned if the first argument is not a string, the second argument is not a number or when the third argument is present and is not zero or one.

See byteidx() and byteidxcomp() for getting the byte index from the character index and utf16idx() for getting the UTF-16 index from the character index. Refer to string-offset-encoding for more information. Examples:

echo charidx('áb́ć', 3)               returns 1
echo charidx('áb́ć', 6, 1)    returns 4
echo charidx('áb́ć', 16)              returns -1
echo charidx('a😊😊', 4, 0, 1)    returns 2

Can also be used as a method:

GetName()->charidx(idx)

Parameters

denops: Denops
string: unknown
idx: unknown
optional
countcc: unknown
optional
utf16: unknown

Returns

Promise<number>