Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove a non-breaking space in Ruby

Tags:

ruby

People also ask

How do you remove spaces in Ruby?

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 .

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 ).

How do you check whitespace in Ruby?

If you are using Rails, you can simply use: x. blank? This is safe to call when x is nil, and returns true if x is nil or all whitespace.

How do you delete a new line character in Ruby?

Often we want to remove extraneous characters from the end of a string. The prime example is a newline on a string read from input. The chop method removes the last character of the string (typically a trailing newline character). If the character before the newline is a carriage return (\r), it will be removed also.


irb(main):001:0> d = "foo\u00A0\bar"
=> "foo \bar"
irb(main):002:0> d.gsub("\u00A0", "")
=> "foo\bar"

In case you do not care about the non-breaking space specifically, but about any "special" unicode whitespace character that might appear in your string, you can replace it using the POSIX bracket expression for whitespace:

s.gsub(/[[:space:]]/, '')

These bracket expressions (as opposed to matchers like \s) do not only match ASCII characters, but all unicode characters of a class.

For more details see the ruby documentation


It's an old thread but maybe it helps somebody. I found myself looking for a solution to the same problem when I discovered that strip doesn't do the job. I checked with method ord what the character was and used chr to represent it in gsub

2.2.3 :010 > 160.chr("UTF-8")
 => " " 
2.2.3 :011 > 160.chr("UTF-8").strip
 => " " 
2.2.3 :012 > nbsp = 160.chr("UTF-8")
 => " " 
2.2.3 :013 > nbsp.gsub(160.chr("UTF-8"),"")
 => ""

I couldn't understand why strip doesn't remove something that looked like a space to me so I checked here what ASCII 160 actually is.


d.gsub("\u00A0", "") does not work in Ruby 1.8. Instead use d.gsub(/\302\240/,"")

See http://blog.grayproductions.net/articles/understanding_m17n for lots more on the character encoding differences between 1.8 and 1.9.