简体   繁体   English

为什么我的Ruby字符串两次显示转义字符反斜杠('\\')?

[英]Why does my Ruby string show the escape character backslash ('\') twice?

I'm using \\.br\\ as delimiter: 我使用\\.br\\作为分隔符:

[10, 20, 30].join('\.br\\')

expected result: 预期结果:

"10\.br\20\.br\30"

what is actually returned: 实际返回的内容:

"10\\.br\\20\\.br\\30"

I added escape for the backslash, how could I get the expected result? 我为反斜杠添加了转义符,如何获得预期的结果?

I think you'll find that the backslashes are not in fact doubled in the string. 我想您会发现反斜杠实际上并未在字符串中加倍。
To check, instead of printing it using p (which uses String#inspect ) just print it using puts . 要进行检查,而不是使用p (使用String#inspect )进行打印,而只需使用puts打印即可。

When the string is inspect ed it uses double-quotes, and tries to produce a version of the string that you can copy and paste in to Ruby to get the same string - so it needs to double up the backslash characters. inspect字符串时,它会使用双引号,并尝试生成该字符串的一个版本,您可以将其复制并粘贴到Ruby中以获得相同的字符串-因此它需要将反斜杠字符加倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM