In ruby, how to find the index of first non-whitespace (non-tab, non-space, non-newline) character of given string.
在ruby中,如何查找给定字符串的第一个非空格(非制表符、非空格、非换行符)字符的索引。
For example, given string "\t\nstring"
, the index of first non-tab, non-space, non-newline character will be 2 which is 's'.
例如,给定字符串“\t\nstring”,第一个非选项卡、非空格、非换行符的索引将是2,即“s”。
2 个解决方案
#1
6
With this notation:
这个符号:
/\S/ =~ "\t\nstring"
# => 2
#2
2
Try this one. s
is your string
试试这个。年代你的字符串
s.index(s.lstrip[0])
#1
6
With this notation:
这个符号:
/\S/ =~ "\t\nstring"
# => 2
#2
2
Try this one. s
is your string
试试这个。年代你的字符串
s.index(s.lstrip[0])