Ruby: Removing Whitespace

Often times it might be necessary to remove whitespaces from your string. There are two different kinds of whitespaces Sometimes, you might find yourself dealing with a combination of leading and trailing whitespaces. An example of that would be "  hello world ".
 * Leading Whitespace- An example of that would be "     hello"
 * Trailing Whitespace - An example of that would be "hello    "

Ruby offers wonderful methods to deal with removing whitespaces.

Removing Leading Whitespaces - You can remove leading whitespaces from a string using that string's inbuilt #lstrip and #lstrip! methods. Let us take a look at an example



Removing Trailing Whitespaces - You can remove remove trailing whitespaces from your string using the string's inbuilt #rstrip and #rstrip! methods. Let us take a look at an example



Remove All Whitespaces - You can remove all the whitespaces from your string using the string's inbuilt #strip method. This method will remove both leading and trailing whitespaces. Let us take a look at an example



Please read the documentation to know more about these methods. You can access the documentation at http://www.ruby-doc.org/core-1.9.3/String.html