Open In App

How to Remove Whitespace in Ruby?

Last Updated : 08 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to remove whitespace in Ruby. Removing whitespace in Ruby eliminates spaces, tabs, carriage returns, and newlines from a string.

Using gsub Method with a Regular Expression

  1. In this approach, we are using the gsub method with a regular expression \s+ in Ruby.
  2. This regular expression matches one or more whitespace characters.
  3. Replacing these matches with an empty string effectively removes all whitespace from the input string uInput. The result is then printed as the space-removed string.

In the below example, the gsub method with a regular expression is used to remove whitespace in ruby.

Ruby
uInput = "  Hello,  world!  "
res = uInput.gsub(/\s+/, "")
puts "Original String: #{uInput}"
puts "Space Removed String: #{res}"

Output:

Screenshot-2024-03-27-at-15-19-24-428gvv97p---Ruby---OneCompiler

Using Delete Method with Specific Characters

  1. In this approach, we are using the delete method in Ruby to remove specific characters from a string.
  2. The characters specified within the delete method (” \t\r\n”) represent whitespace characters such as spaces, tabs, carriage returns, and newlines.
  3. The resulting string res contains the original string uInput with these specified characters removed.

In the below example the delete method with specific characters is used to remove whitespace in ruby.

Ruby
uInput = "  Hello,  GeeksforGeeks!  "
res = uInput.delete(" \t\r\n")
puts "Original String: #{uInput.inspect}"
puts "Space Removed String: #{res.inspect}"

Output:

Screenshot-2024-03-27-at-15-22-00-428gvv97p---Ruby---OneCompiler

Using the Split and Join Methods

  1. In this approach, we are using the split method to break the input string uInput into an array of words based on whitespace.
  2. Then, we use the join method to concatenate the array elements back together without any spaces, creating the space-removed string res.

In the below example the split and join methods are used to remove whitespace in ruby.

Ruby
uInput = "  Hello,  GFG!  "
res = uInput.split.join("")
puts "Original String: #{uInput.inspect}"
puts "Space Removed String: #{res.inspect}"

Output:

Screenshot-2024-03-27-at-15-26-48-428gwm7q6---Ruby---OneCompiler

Using Strip Method

  1. In this approach, we are using the strip method in Ruby, which removes leading and trailing whitespace from the input string.
  2. By applying uInput.strip, we eliminate the spaces at the beginning and end of the string ” Hello, GFG! “, resulting in the modified string “Hello, GFG!” without leading or trailing spaces.

In the below example the strip method is used to remove whitespace in ruby.

Ruby
uInput = "                  Hello,  GFG!  "
res = uInput.strip
puts "Original String: #{uInput.inspect}"
puts "Space Removed String: #{res.inspect}"

Output:

Screenshot-2024-03-28-at-17-11-23-428m5vq6j---Ruby---OneCompiler



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads