{

How to remove white spaces in Ruby with examples| delete leading and trail blank spaces


This tutorial explains how to remove whitespaces from a given string in Ruby.

  • How to strip whitespaces in a string?
  • How do remove from leading and trailing of a string?

How to remove white spaces at begin and end of a string

String strip method removes the whitespaces from the start and end of a string.

result=" welcome to my website ".strip
puts "-#{result}-"

Output:

-welcome to my website-

String lstrip method removes the whitespaces from starting a string.

result=" welcome to my website ".lstrip
puts "#{result}"
-welcome to my website -

String rstrip method removes the whitespaces from the end of a string.

result=" welcome to my website ".lstrip
puts "#{result}"
- welcome to my website-

How to strip whitespaces in a string?

delete method in a string removes all given parameters from a string and returns result string. Here is an example

result=" welcome to my website ".delete(" ")
puts "#{result}"

Output:

welcometomywebsite
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

How to install and uninstall gem in Ruby Programming| Ruby on Rails by Example

How to do sum of array numbers in Ruby with examples

How to check the type of a variable is Ruby| Ruby on Rails By Example

How to convert Class or Hash Object to JSON Object Ruby| Ruby on Rails By Example

How to remove duplicate elements from an array in Ruby with examples

How to Convert current Unix timestamp epoch to DateTime in Ruby Programming| Ruby on Rails by Example

How to remove an element from an array in Ruby Programming| Ruby on Rails by Example