简体   繁体   中英

Output string containing escape characters

I have a string:

s = "\t\n"

I intend to output:

\t\n

When I do

puts s

I see nothing. How should I do it?

That's because \\n and \\t are escape sequences

A single-quoted strings don't process ASCII escape codes, and they don't do string interpolation while double-quoted does both.

So if you want that behaviour, you should do

s = '\t\n'

Just use inspect to see the "raw" escape sequences.

s = "\t\n"
puts s.inspect
# >> "\t\n"
p s # >> "\t\n"

要么

puts s.inspect[1...-1] # >> \t\n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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