Ken*_*ent 10
它像python一样工作:
echo '0123456'[2:4]
234
Run Code Online (Sandbox Code Playgroud)
详细文档:
:h expr-[:]
Run Code Online (Sandbox Code Playgroud)
VimL 表达式mystring[a:b]返回从字节索引a到(包括)的子字符串b。
但请注意,其语义与 Python 的下标表示法或 Javascript 的str.slice()不同。b特别是,VimL 计算字节数而不是字符数以进行索引,并且包括范围末尾的字节(索引处的字节)。
这些示例说明了行为的不同之处:
\n VimL Python\n--------------------------------\ns = "abcdef"\ns[0] a a\ns[5] f f\ns[0:1] ab a\ns[2:4] cde cd\ns[-1:] f f\ns[:-1] abcdef abcde\ns[-2:-1] ef e\n\ns = "\xc3\xa4\xc3\xb6\xc3\xbc"\ns[0:2] \xc3\xa4\\xc3 \xc3\xa4\xc3\xb6\nRun Code Online (Sandbox Code Playgroud)\n:help subscript另外,请查找和上的文档:help expr-[:]。
对于多字节字符,还有 :h strcharpart()
如果该函数不存在于您的旧版 Vim 中,则可以使用例如
function! lh#encoding#strpart(mb_string, p, l)
" call lh#assert#value(lh#encoding#strlen(a:mb_string)).is_ge(a:p+a:l)
return matchstr(a:mb_string, '.\{,'.a:l.'}', 0, a:p+1)
endfunction
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1673 次 |
| 最近记录: |