Is there any substr()
like function to get substring in vim script? If not what is the best replacement or alternative for this kind of task?
The VimL expression mystring[a:b]
returns a substring from byte index a
up to (and including) b
.
But be aware that the semantics are different from Python's subscript notation or Javascript's str.slice()
. In particular, VimL counts the bytes instead of the characters for indexing, and the byte at the end of the range (the byte at index b
) is included.
These examples illustrate how the behavior differs:
VimL Python
--------------------------------
s = "abcdef"
s[0] a a
s[5] f f
s[0:1] ab a
s[2:4] cde cd
s[-1:] f f
s[:-1] abcdef abcde
s[-2:-1] ef e
s = "äöü"
s[0:2] ä\xc3 äö
Also, look up the documentation at :help subscript
and :help expr-[:]
.
it works like python:
echo '0123456'[2:4]
234
For detailed doc:
:h expr-[:]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With