[Ruby] Removing Half-width and Full-width Spaces from Strings

Tadashi Shigeoka ·  Tue, January 29, 2013

Here’s a Ruby snippet to remove all half-width and full-width spaces from strings.

Ruby

'a b c'.gsub(/(\\s| )+/, '')
 => "abc" 

What this does is use the gsub method with a regular expression to replace half-width or full-width spaces with empty strings.

That’s all from the Gemba.