Why does strip not remove the leading whitespace?

Severin picture Severin · Nov 30, 2013 · Viewed 8.8k times · Source

I tried to strip the leading whitespace of a string:

" Bagsværd".strip # => " Bagsværd"

I expect it to return "Bagsværd" instead.

Answer

matt picture matt · Nov 30, 2013

Where did the string " Bagsværd" come from?

It’s likely that the space character at the start of the string is not a “normal” space, but a non-breaking space (U+00A0):

2.0.0p353 :001 > " Bagsværd".strip
 => "Bagsværd" 
2.0.0p353 :002 > "\u00a0Bagsværd".strip
 => " Bagsværd" 

You could remove it with gsub rather than strip:

2.0.0p353 :003 > "\u00a0Bagsværd".gsub(/\A\p{Space}*/, '')
 => "Bagsværd" 

This uses the \A anchor, and the \p{Space} character property to emulate lstrip. To strip both leading and trailing whitespace, use:

2.0.0p353 :007 > "\u00a0Bagsværd\u00a0".gsub(/\A\p{Space}*|\p{Space}*\z/, '')
 => "Bagsværd"