See if a ruby string has whitespace in it -
I have to check whether there is any white space in the string or not. What is the most effective way to do this in Ruby?
Thank you
If by "white space" you mean in the regular expression sense Any space alphabet, tab, newline, carriage return or (I think) form-feed, if any, any given answer will work:
s.match (/ Code> s [/ \ s /]
If you are interested in checking only one space character, then your primary Try it
s.match ("") s.index ("") s = ~ / / s ["]
IRB ( Ruby 1.8.6) to:
s = "ab" puts s.match (/ \ s /)? "Yes": "no" # -> Yes s.index "Yes": "no": "no" # - & gt; yes say s = ~ / \ s /? "Yes": "no" # - & gt; yes it puts [/ \ "Yes": "no": "no": - Yes s = "abc" s.match (/ \ s /)? "Yes": "no" # - & gt; not s.index (/ \ S /)? "Yes": "No" # - & gt; Does not put s = ~ / \ s /? "Yes": "no" # - & gt; Someone puts [/ \ s /]? "Yes": "no" # - & gt; No
Comments
Post a Comment