简体   繁体   中英

In ruby, how to find the index of first non-whitespace character of given string

In ruby, how to find the index of first non-whitespace (non-tab, non-space, non-newline) character of given string.

For example, given string "\\t\\nstring" , the index of first non-tab, non-space, non-newline character will be 2 which is 's'.

With this notation:

/\S/ =~ "\t\nstring"
# => 2

Try this one. s is your string

s.index(s.lstrip[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM