Hi
How can I remove all whitespaces from a string. even whitespace inside
the string.
Thanks
Hi
How can I remove all whitespaces from a string. even whitespace inside
the string.
Thanks
If you want to eliminate all spaces, you can use String#gsub.
" a b c “.gsub(” ", “”) #=> “abc”
However, if you want to eliminate leading and trailing spaces and
collapse
several internal spaces to one, you can combine String#squeeze and
String#strip.
" a b c ".squeeze.strip #=> “a b c”
Regards,
Craig
Thanks of the tip on #squeeze Craig! I didn’t know about that method.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs