如何在Ruby中使用反斜杠打印字符串

时间:2021-05-29 21:19:03

I have a string str = "xyz\123" and I want to print it as is.

我有一个字符串str =“xyz \ 123”,我想按原样打印它。

The IRB is giving me an unexpected output. Please find the same below:-

IRB给了我意想不到的输出。请在下面找到相同的内容: -

1.9.2p290 :003 > str = "xyz\123"
 => "xyzS" 
1.9.2p290 :004 > 

Any ideas on how can I get IRB to print the original string i.e. "xyz\123".

关于如何让IRB打印原始字符串即“xyz \ 123”的任何想法。

Thank you..

UPDATE :

I tried escaping it , but it doesn't seem to be that simple for some reason. Please find below my trials with the same:

我试图逃避它,但由于某种原因它似乎并不那么简单。请在下面找到我的试用版:

1.9.2p290 :004 > str = "xyz'\'123"
 => "xyz''123" 
1.9.2p290 :005 > str = "xyz'\\'123"
 => "xyz'\\'123" 
1.9.2p290 :006 > str = "xyz'\\\'123"
 => "xyz'\\'123" 
1.9.2p290 :007 > 

4 个解决方案

#1


5  

UPDATED answer:

escape token '\' is always working in plain ruby code, but not always working in "ruby console". so I suggest you write a unit test:

转义标记'\'始终使用普通的ruby代码,但并不总是在“ruby控制台”中工作。所以我建议你写一个单元测试:

# escape_token_test.rb 
require 'test/unit'
class EscapeTokenTest < Test::Unit::TestCase
  def test_how_to_escape
    hi = "hi\\backslash"
    puts hi
  end 
end

and you will get result as:

你会得到如下结果:

hi\backslash

and see @pst's comment.

并看到@ pst的评论。

#2


3  

The backslash character is an escape character. You may have seen "\n" be used to display a new line, and that is why. "\123" evaulates the ASCII code for 83, which is "S". To print a backslash use 2 backslashes. So you could use str = "xyz\\123".

反斜杠字符是转义字符。您可能已经看到“\ n”用于显示新行,这就是原因。 “\ 123”表示83的ASCII码,即“S”。要打印反斜杠,请使用2个反斜杠。所以你可以使用str =“xyz \\ 123”。

#3


0  

How to print a backslash?

如何打印反斜杠?

Use 2 backslashes, e.g. "xyz\\123"

使用2个反斜杠,例如“XYZ \\ 123”

Why does "xyz\123" evaluate to "xyzS"?

为什么“xyz \ 123”评价为“xyzS”?

In a double-quoted string, \nnn is an octal escape.

在双引号字符串中,\ nnn是八进制转义符。

如何在Ruby中使用反斜杠打印字符串

Thomas, D. (2009) Programming Ruby, p.329

Thomas,D。(2009)Programming Ruby,p.329

So, octal 123
= (64 * 1) + (8 * 2) + 3
= decimal 83
= ASCII S

因此,八进制123 =(64 * 1)+(8 * 2)+ 3 =十进制83 = ASCII S

#4


0  

It's simple ... try dump function:

这很简单...尝试转储功能:

mystring = %Q{"Double Quotes"}
p mystring.dump
=> "\"\\\"Double Quotes\\\"\""
p mystring
=>"\"Double Quotes\""

#1


5  

UPDATED answer:

escape token '\' is always working in plain ruby code, but not always working in "ruby console". so I suggest you write a unit test:

转义标记'\'始终使用普通的ruby代码,但并不总是在“ruby控制台”中工作。所以我建议你写一个单元测试:

# escape_token_test.rb 
require 'test/unit'
class EscapeTokenTest < Test::Unit::TestCase
  def test_how_to_escape
    hi = "hi\\backslash"
    puts hi
  end 
end

and you will get result as:

你会得到如下结果:

hi\backslash

and see @pst's comment.

并看到@ pst的评论。

#2


3  

The backslash character is an escape character. You may have seen "\n" be used to display a new line, and that is why. "\123" evaulates the ASCII code for 83, which is "S". To print a backslash use 2 backslashes. So you could use str = "xyz\\123".

反斜杠字符是转义字符。您可能已经看到“\ n”用于显示新行,这就是原因。 “\ 123”表示83的ASCII码,即“S”。要打印反斜杠,请使用2个反斜杠。所以你可以使用str =“xyz \\ 123”。

#3


0  

How to print a backslash?

如何打印反斜杠?

Use 2 backslashes, e.g. "xyz\\123"

使用2个反斜杠,例如“XYZ \\ 123”

Why does "xyz\123" evaluate to "xyzS"?

为什么“xyz \ 123”评价为“xyzS”?

In a double-quoted string, \nnn is an octal escape.

在双引号字符串中,\ nnn是八进制转义符。

如何在Ruby中使用反斜杠打印字符串

Thomas, D. (2009) Programming Ruby, p.329

Thomas,D。(2009)Programming Ruby,p.329

So, octal 123
= (64 * 1) + (8 * 2) + 3
= decimal 83
= ASCII S

因此,八进制123 =(64 * 1)+(8 * 2)+ 3 =十进制83 = ASCII S

#4


0  

It's simple ... try dump function:

这很简单...尝试转储功能:

mystring = %Q{"Double Quotes"}
p mystring.dump
=> "\"\\\"Double Quotes\\\"\""
p mystring
=>"\"Double Quotes\""