English 中文(简体)
• 如何在鲁比拉用后向下lash印地毯
原标题:How to print a string with a backward slash in Ruby

我有“钟表”=“xyz123”,我希望照样打印。

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

1.9.2p290 :003 > str = "xyz123"
 => "xyzS" 
1.9.2p290 :004 > 

关于我如何能够让国际职工会印刷原体,即“xyz123”。

谢谢。

UPDATE :

我试图放弃它,但出于某种原因,这似乎并不简单。 请参看下文所述审判:

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 > 
最佳回答

UPDATED的答复:

逃跑总是在简易废墟法中工作,但并不总是在“红".”中工作。 因此,我建议你写一个单位测试:

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

并且将产生以下结果:

hiackslash

参看@pst的评论。

问题回答

The backslash character is an escape character. You may have seen " " 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".

www.un.org/spanish/ecosoc 如何打印回击?

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

www.un.org/spanish/ecosoc 为什么xyz123>>?

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

“Table

Thomas, D.(2009年),《方案规划》,Ruby, p.329

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

它是简单的......审判倾销功能:

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




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签