is there a built in way to get first match or nil for a character from string.
i can get a first character like this
'word'.match('o').to_s
but if there is no match it gives ""
not nil
Use String#[match_str]
:
If a
match_str
is given, that string is returned if it occurs in the string.Returns
nil
if (...) the match string cannot be found.
'word'['o'] #=> 'o'
'word'['q'] #=> nil
The []
method is very versatile, you can also pass a regular expression or indices in different ways.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments