Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby function to remove all white spaces?

Tags:

string

ruby

What is the Ruby function to remove all white spaces? I'm looking for something kind of like PHP's trim()?

like image 527
gpwu Avatar asked Oct 28 '09 01:10

gpwu


People also ask

Which removes all leading whitespace?

The lstrip() method will remove leading whitespaces, newline and tab characters on a string beginning.

Which function is used to remove white spaces only from start?

Method 1: Using ltrim() Method: The ltrim() method is used to strip whitespace only from the beginning of a string.

How do you strip a string in Ruby?

The . strip method removes the leading and trailing whitespace on strings, including tabs, newlines, and carriage returns ( \t , \n , \r ).


1 Answers

If you want to remove only leading and trailing whitespace (like PHP's trim) you can use .strip, but if you want to remove all whitespace, you can use .gsub(/\s+/, "") instead .

like image 87
joel.neely Avatar answered Sep 20 '22 11:09

joel.neely