Home > Net >  How Can I Search for a Character in a String in Vim Script
How Can I Search for a Character in a String in Vim Script

Time:08-27

I am writing a function in Vim script that takes one argument. It could be either a comma separated list of numbers (e.g. 1,2,3), or a range of numbers (e.g. 1-3). I want to check if the second character in the argument is a - symbol so that I can know that it is a range of numbers. Here is code:

function! MyFunction(...)
    if second_char(a:1) == '-'
        echo "Hyphen"
    else
        echo "Comma"
    endif
endfunction

command -nargs=* MyFunction call MyFunction(<f-args>)

I read the documentation for the search function but it is honestly really confusing.

CodePudding user response:

So it's really as simple as using brackets. Here is an example, where MY_STRING is the string you're using:

if MY_STRING[1] == '-'
    echo "Hyphen"
else
    echo "Comma"
endif
  • Related