Skip to main content
Module

x/denops_std/function/mod.ts>byteidx

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

Return byte index of the {nr}'th character in the String {expr}. Use zero for the first character, it then returns zero. If there are no multibyte characters the returned value is equal to {nr}. Composing characters are not counted separately, their byte length is added to the preceding base character. See byteidxcomp() below for counting composing characters separately. When {utf16} is present and TRUE, {nr} is used as the UTF-16 index in the String {expr} instead of as the character index. The UTF-16 index is the index in the string when it is encoded with 16-bit words. If the specified UTF-16 index is in the middle of a character (e.g. in a 4-byte character), then the byte index of the first byte in the character is returned. Refer to string-offset-encoding for more information. Example :

echo matchstr(str, ".", byteidx(str, 3))

will display the fourth character. Another way to do the same:

let s = strpart(str, byteidx(str, 3))
echo strpart(s, 0, byteidx(s, 1))

Also see strgetchar() and strcharpart().

If there are less than {nr} characters -1 is returned. If there are exactly {nr} characters the length of the string in bytes is returned. See charidx() and utf16idx() for getting the character and UTF-16 index respectively from the byte index. Examples:

echo byteidx('a😊😊', 2)  returns 5
echo byteidx('a😊😊', 2, 1)       returns 1
echo byteidx('a😊😊', 3, 1)       returns 5

Can also be used as a method:

GetName()->byteidx(idx)

Parameters

denops: Denops
expr: unknown
nr: unknown
optional
utf16: unknown

Returns

Promise<number>