检查给了我 \\ 但只放了 \

发布于 2024-11-04 19:30:31 字数 352 浏览 2 评论 0原文

为什么两次看跌期权的结果不同?

test_string = "C:/Program Files/TestPro/TestPro Automation Framework/"
puts test_string.gsub("/","\\\\")

#result is : C:\Program Files\TestPro\TestPro Automation Framework\

puts
puts test_string.gsub("/","\\\\") .inspect

#result as desired : C:\\Program Files\\TestPro\\TestPro Automation Framework\\

why do I have different results from both puts?

test_string = "C:/Program Files/TestPro/TestPro Automation Framework/"
puts test_string.gsub("/","\\\\")

#result is : C:\Program Files\TestPro\TestPro Automation Framework\

puts
puts test_string.gsub("/","\\\\") .inspect

#result as desired : C:\\Program Files\\TestPro\\TestPro Automation Framework\\

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小梨窩很甜 2024-11-11 19:30:31

Ruby 的 String.inspect 转义所有特殊字符,这就是为什么你会看到“\\” 与 .inspect

请参阅 String.inspect source 这里

if (c == '"'|| c == '\\' ||
    (c == '#' &&
     p < pend &&
     MBCLEN_CHARFOUND_P(rb_enc_precise_mbclen(p,pend,enc)) &&
     (cc = rb_enc_codepoint(p,pend,enc),
      (cc == '

基本上,if c == '\',将“\”连接到它,所以它变成了“\\< /code>"

如果你想双重转义反斜杠,你需要尝试使用

test_string = "C:/Program Files/TestPro/TestPro Automation Framework/"
puts test_string.gsub("/","\\\\\\\\")

#C:\\Program Files\\TestPro\\TestPro Automation Framework\\
|| cc == '@' || cc == '{')))) { if (p - n > prev) str_buf_cat(result, prev, p - n - prev); str_buf_cat2(result, "\\"); prev = p - n; continue; }

基本上,if c == '\',将“\”连接到它,所以它变成了“\\< /code>"

如果你想双重转义反斜杠,你需要尝试使用

Ruby's String.inspect escape all special characters, thats why you seee "\\" with .inspect

See String.inspect source here

if (c == '"'|| c == '\\' ||
    (c == '#' &&
     p < pend &&
     MBCLEN_CHARFOUND_P(rb_enc_precise_mbclen(p,pend,enc)) &&
     (cc = rb_enc_codepoint(p,pend,enc),
      (cc == '

basically, if c == '\', concatenate "\" to it, so it became "\\"

If you want double escape the backslash, you need to try with

test_string = "C:/Program Files/TestPro/TestPro Automation Framework/"
puts test_string.gsub("/","\\\\\\\\")

#C:\\Program Files\\TestPro\\TestPro Automation Framework\\
|| cc == '@' || cc == '{')))) { if (p - n > prev) str_buf_cat(result, prev, p - n - prev); str_buf_cat2(result, "\\"); prev = p - n; continue; }

basically, if c == '\', concatenate "\" to it, so it became "\\"

If you want double escape the backslash, you need to try with

远山浅 2024-11-11 19:30:31

puts 将返回第一个斜杠作为转义符号。 Inspect 不会触发转义斜杠,因此它显示原始字符串。

string = "\\Hello World!\\"
puts string
#=> "\Hello World!\"
string
#=> "\\Hello World!\\"

因此,如果您尝试这样做,它会起作用:

puts "I am in \"Dog Bar\" now"
#=> "I am in "Dog Bar" now"
"I am in \"Dog Bar\" now"
#=> "I am in \"Dog Bar\" now"
"I am in "Dog Bar" now"
#=> SyntaxError: compile error

puts will return first slashes as an escape symbol. Inspect won't trigger escape slashes, so it shows original string.

string = "\\Hello World!\\"
puts string
#=> "\Hello World!\"
string
#=> "\\Hello World!\\"

So if you will try this it will work:

puts "I am in \"Dog Bar\" now"
#=> "I am in "Dog Bar" now"
"I am in \"Dog Bar\" now"
#=> "I am in \"Dog Bar\" now"
"I am in "Dog Bar" now"
#=> SyntaxError: compile error
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文