条带在导轨中无法正常工作

Has*_*mad 1 ruby regex ruby-on-rails strip

我有一个字符串

s = "        \r\n Displays the unique ID number assigned to the\r\nAlias Person."
Run Code Online (Sandbox Code Playgroud)

我想删除这个起始空间,但它甚至不删除这些引号。

我试过

s = s.strip!
s = s.gsub!('"','')
Run Code Online (Sandbox Code Playgroud)

Seb*_*lma 5

您的字符串中包含不间断的空格,如果您在编辑器中不使用任何帮助,则区别是不可见的:

p "        \r\n Displays the unique ID number assigned to the\r\nAlias Person."
p "        \r\n Displays the unique ID number assigned to the\r\nAlias Person."
Run Code Online (Sandbox Code Playgroud)

但是,如果您映射字符串中的每个字符,您就可以看到不同之处:

[32, 32, 32, 32, 32, 32, 32, 32, 13, 10, 32, ...]
[160, 160, 160, 160, 160, 160, 160, 160, 13, 10, 32, ...]
Run Code Online (Sandbox Code Playgroud)

那些 160 是您无法替换的,您必须手动删除它们,或者通过拒绝匹配 160 的那些然后加入并再次转换:

string = "        \r\n Displays the unique ID number assigned to the\r\nAlias Person."
p string.chars.reject { |char| char.ord == 160 }.join
# "\r\n Displays the unique ID number assigned to the\r\nAlias Person."
Run Code Online (Sandbox Code Playgroud)