VimL string index

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

VimL string index

Mark Volkmann-2
I don't see a builtin function to find the index of a substring within a string.
Does that exist?
For example, I want something like index('foobarbaz', 'bar') to return 3.
It seems the index function works on lists, but not strings.

---
R. Mark Volkmann
Object Computing, Inc.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: VimL string index

Marcin Szamotulski
On 05:39 Mon 04 May     , Mark Volkmann wrote:
> I don't see a builtin function to find the index of a substring within a string.
> Does that exist?
> For example, I want something like index('foobarbaz', 'bar') to return 3.
> It seems the index function works on lists, but not strings.
>
> ---
> R. Mark Volkmann
> Object Computing, Inc.

Just check `:h string-functions`, and you'll find `stridx()`.

Best regards,
Marcin

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: VimL string index

Jürgen Krämer-4
In reply to this post by Mark Volkmann-2

Hi,

Mark Volkmann schrieb am 04.05.2015 um 12:39:
> I don't see a builtin function to find the index of a substring within a string.
> Does that exist?
> For example, I want something like index('foobarbaz', 'bar') to return 3.
> It seems the index function works on lists, but not strings.

the function is called stridx(). Note that it returns the *byte* index. This
might matter if you have encoding set to UTF-8 and the string in which you
want to search contains non-ASCII letters before the first occurrence of the
search string, e.g.,

  :echo stridx('Jürgen', 'rg')

prints 2 if encoding is set to latin1, but prints 3 if encoding is set to
UTF-8.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.