how to insert a carriage return in a string

E

Emmanuel Touzery

Hello,

definitely a very newbie question... i have strings, possibly with
carriage returns in my application, and i want to save them with '\n' in
litteral in my file (so that i'm guaranteed this string is on at most one
line), and then get back "\n" when deserializing.

But i don't quite get it for the deserialization part:

irb(main):001:0> myString = 'a\nb'
"a\\nb"
irb(main):002:0> myString.gsub('\n', "\n")
"a\\nb"
irb(main):003:0> myString.gsub("\\n", "\n")
"a\\nb"

obviously i want to get "a\nb" (from "a\\nb"). How I am supposed to do that?

thank you,

emmanuel
 
E

Edwin Pratomo

irb(main):003:0> myString.gsub("\\n", "\n")
"a\\nb"

obviously i want to get "a\nb" (from "a\\nb"). How I am supposed to do
that?

myString.gsub(/\\n/, "\n")

rgds,
Edwin.
 
B

Bermejo, Rodrigo

You should use doublequotes to build your strings
Check the differences btw " & '



irb.log(main):001:0> f=File.open "tmp" ,"w"
irb.log(main):002:0> myString = 'a\nb'
"a\\nb"
irb.log(main):003:0> f << myString
irb.log(main):005:0> f.close
irb.log(main):007:0> r=`cat tmp` #Or File.open
"a\\nb"


irb.log(main):011:0> myString = "a\nb"
"a\nb"
irb.log(main):012:0> f=File.open "tmp" ,"w"
irb.log(main):013:0> f << myString
irb.log(main):014:0> f.close
irb.log(main):016:0> r=`cat tmp` #Or File.open
"a\nb"

then you can >>>
r.split("\n").each {| line | p line }


-ronnie.

Emmanuel said:
Hello,

definitely a very newbie question... i have strings, possibly with
carriage returns in my application, and i want to save them with '\n' in
litteral in my file (so that i'm guaranteed this string is on at most one
line), and then get back "\n" when deserializing.

But i don't quite get it for the deserialization part:

irb(main):001:0> myString = 'a\nb'
"a\\nb"
irb(main):002:0> myString.gsub('\n', "\n")
"a\\nb"
irb(main):003:0> myString.gsub("\\n", "\n")
"a\\nb"

obviously i want to get "a\nb" (from "a\\nb"). How I am supposed to do that?

thank you,

emmanuel

--
General Electric - CIAT
Advanced Engineering Center
________________________________________
Rodrigo Bermejo
Information Technologies.
Dial-comm : *879-0644
Phone :(+52) 442-196-0644
mailto:[email protected]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top