Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby 1.9 strip not removing whitespace

Tags:

ruby-1.9

Im doing some screen scraping and im getting back a string that appears to end with whitespace but neither string.strip or strip.gsub(/\s/u, '') removes the character.

Im guessing it's a character encoding issue. Any suggestions?

like image 233
Sam Avatar asked Oct 12 '10 10:10

Sam


3 Answers

I think, there are a lot of "space characters". You can use something like this:

my_string.gsub("\302\240", ' ').strip
like image 153
taro Avatar answered Nov 01 '22 13:11

taro


You can try this: my_string.gsub(/\A[[:space:]]+|[[:space:]]+\z/, '')

This should remove all space characters from the beginning and the end of string, including all possible unicode space variations.

like image 44
Pavel Pravosud Avatar answered Nov 01 '22 12:11

Pavel Pravosud


Figure out the character code of the last character (str[-1].ord) and explicitly search and destroy it. Rinse/repeat if there exist more unwanted characters after that. After doing this, report back here what the invisible character was. (Perhaps it's only invisible because the font you are using does not have that glyph?)

like image 2
Phrogz Avatar answered Nov 01 '22 12:11

Phrogz