msg => "!iex [email protected]|[email protected]> Firs-6ren">
gpt4 book ai didi

Ruby 声称 ""实际上不是一个空格?

转载 作者:行者123 更新时间:2023-12-02 18:59:17 28 4
gpt4 key购买 nike

我正在尝试解决为什么 Ruby 没有用空格分割我的字符串。例如:

[1] pry(#<irb>)> msg
=> "!iex <http://test-domain.com.au|test-domain.com.au> <mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfd9d6cdcccb91d3decccbffcbdacccb92dbd0d2ded6d191dcd0d291deca" rel="noreferrer noopener nofollow">[email protected]</a>|<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d5b544f4e4913515c4e497d49584e49105952505c5453135e5250135c48" rel="noreferrer noopener nofollow">[email protected]</a>> FirstName"
[2] pry(#<irb>)> msg.split(" ")
=> ["!iex <http://test-domain.com.au|test-domain.com.au> <mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="385e514a4b4c1654594b4c784c5d4b4c155c5755595156165b575516594d" rel="noreferrer noopener nofollow">[email protected]</a>|<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2d4dbc0c1c69cded3c1c6f2c6d7c1c69fd6dddfd3dbdc9cd1dddf9cd3c7" rel="noreferrer noopener nofollow">[email protected]</a>> FirstName"]
[3] pry(#<irb>)> msg.include? " "
=> false
[8] pry(#<irb>)> msg.inspect
=> "\"!iex <http://test-domain.com.au|test-domain.com.au> <mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ceae5fefff8a2e0edfff8ccf8e9fff8a1e8e3e1ede5e2a2efe3e1a2edf9" rel="noreferrer noopener nofollow">[email protected]</a>|<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9afa0bbbabde7a5a8babd89bdacbabde4ada6a4a8a0a7e7aaa6a4e7a8bc" rel="noreferrer noopener nofollow">[email protected]</a>> FirstName\""
[9] pry(#<irb>)>

正如您在上面看到的,我的字符串似乎包含空格,但 split 方法无法处理它。我尝试在字符串上运行 inspect 只是为了查看是否显示了其他内容,但这对我来说并没有多大意义。

最佳答案

字符串包含 some other kind of whitespace或者您正在分割其他类型的空白。例如,"foo\u2002bar" 看起来像 foo bar 但包含一个特殊的空格。

尝试msg.dump查看特殊字符。

2.6.5 :008 > msg = "foo\u2002bar"
=> "foo bar"
2.6.5 :009 > msg.dump
=> "\"foo\\u2002bar\""

要在任何空格或制表符上拆分,请在 [[:blank:]] character class 上拆分.

2.6.5 :006 > msg.split(/[[:blank:]]/)
=> ["foo", "bar"]

关于Ruby 声称 ""实际上不是一个空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65799709/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com