OneBite.Dev - Coding blog in a bite size

trim a string in Ruby

Code snippet on how to trim a string in Ruby

  str = "   hello world   "
  str = str.strip   #strip the whitespace from both sides

This code sample will trim the whitespace from both sides of a string. The first line declares the variable ‘str’, which stores the string ” hello world “. The second line stores the result of the ‘.strip’ method called on the variable ‘str’ in the variable ‘str’. The ‘.strip’ method removes all leading and trailing whitespace from the string, leaving just “hello world” in the variable ‘str’.

ruby