Ruby 1.9 strip not removing whitespace

*爱你&永不变心* 提交于 2020-02-20 08:49:28

问题


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?


回答1:


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

my_string.gsub("\302\240", ' ').strip



回答2:


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.




回答3:


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?)



来源:https://stackoverflow.com/questions/3913900/ruby-1-9-strip-not-removing-whitespace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!