Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does strip not remove the leading whitespace?

I tried to strip the leading whitespace of a string:

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

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

like image 388
Severin Avatar asked Nov 30 '13 21:11

Severin


People also ask

Which function used to remove all leading and trailing whitespace in string?

strip(): returns a new string after removing any leading and trailing whitespaces including tabs ( \t ). rstrip(): returns a new string with trailing whitespace removed. It's easier to remember as removing white spaces from “right” side of the string.

Why strip function is not working in Python?

Strip() For Invalid Data Type:The strip() method in python does support only the 'String' data type. This means that the strip() will work only on the string and not any other data type.

How do you remove the leading whitespace in a string?

strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.


3 Answers

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" 
like image 199
matt Avatar answered Oct 17 '22 17:10

matt


The first character in your string is not whitespace

" Bagsværd".bytes
[194, 160, 66, 97, 103, 115, 118, 195, 166, 114, 100]

" Bagsværd".chars[0].ord
 => 160

This is U+00A0 no-break space. Note I could tell this because the editable form of the question preserves the character (whilst anyone trying to cut and paste from the rendered SO post would not be able to replicate your problem)

like image 4
Neil Slater Avatar answered Oct 17 '22 19:10

Neil Slater


The most likely way that strip isn't removing a space, is when it isn't really a space, but is a non-breaking space.

Try this on your machine:

# encoding: utf-8
" Bagsværd".chars.map(&:ord)

On mine, using Ruby 2.0.0p353:

# => [160, 66, 97, 103, 115, 118, 230, 114, 100]
like image 3
the Tin Man Avatar answered Oct 17 '22 18:10

the Tin Man