I tried to strip
the leading whitespace of a string:
" Bagsværd".strip # => " Bagsværd"
I expect it to return "Bagsværd"
instead.
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.
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.
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.
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"
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)
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With