[英]Get length of string using Vimscript
This may be a very basic question, but I can't seem to find an answer with a simple Google search.这可能是一个非常基本的问题,但我似乎无法通过简单的 Google 搜索找到答案。
I simply want to find the length of a string using Vimscript, similar to len() function in Python.我只想使用 Vimscript 查找字符串的长度,类似于 Python 中的 len() 函数。 Is there a simple way to do this in Vimscript, or will I just have to loop over the string?在 Vimscript 中是否有一种简单的方法可以做到这一点,还是我只需要遍历字符串?
There is len
and strlen
.有len
和strlen
。 For strings they are same.对于字符串,它们是相同的。
:echo len("Hello")
:echo strlen("Hello")
Note that the answer by Ashwani only works for single-byte characters, not for multi-byte characters.请注意, Ashwani的答案仅适用于单字节字符,不适用于多字节字符。
There is function strlen()
or len()
in Vim, but they only calculates byte length of a string, instead of character length, like what len()
function in Python does. Vim 中有strlen()
或len()
函数,但它们只计算字符串的字节长度,而不是字符长度,就像 Python 中的len()
函数所做的那样。 We can instead use the strchars()
function to get string length.我们可以改为使用strchars()
函数来获取字符串长度。 I consider this one of the many hidden quirks of Vim.我认为这是 Vim 众多隐藏的怪癖之一。 We just need to get accustomed to it.我们只需要习惯它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.