• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use actual "\n" character in Java?  RSS feed

 
Pradeep Simha
Greenhorn
Posts: 8
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

How to use actual "\n" character in java?
Say I want string to be printed as "This is a test\nThis is a test" without \n begin interpreted as new line character, I just want it to be a part of string not as escape character.
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go for \\n

 
Pradeep Simha
Greenhorn
Posts: 8
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rameshwar Soni wrote:Go for \\n



Thanks, I tried but it works fine if I try to print. But it doesn't work if I try to store it in a string variable. It will always interpret that "\n" as new line character so if I try to print it those two sentences will be printed in new line, I just don't want that to happen. I just want it to store in plain as "This is a test\nThis is a test" also if I try to use that variable it shouldn't interpret "\n" as new line character. Is there anyway I can achieve that in Java?

Background: We are trying to parse some message (which user enters from UI), it updates to DB fine, but while displaying same message on front end we are building JSON out of that data and displaying it in UI. Since if I have "This is a test\nThis is a test" JSON interprets it as new line character and second "This is a test" comes to new line, so it breaks the syntax of JSON
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep Simha wrote:

But it doesn't work if I try to store it in a string variable. It will always interpret that "\n" as new line character so if I try to print it those two sentences will be printed in new line, I just don't want that to happen. I just want it to store in plain as "This is a test\nThis is a test" also if I try to use that variable it shouldn't interpret "\n" as new line character. Is there anyway I can achieve that in Java?


It's working fine for me


Output:
C:\>javac Demo.java

C:\>java Demo
Hello\nBye

C:\>
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep Simha wrote:Thanks, I tried but it works fine if I try to print. But it doesn't work if I try to store it in a string variable. It will always interpret that "\n" as new line character

Did you try it? are you sure? as the previous poster showed, it works fine.

If you have code that does otherwise, show us. Otherwise, there isn't much anyone can do to help.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\ is a special character in Javascript as well, so you may need to escape it twice if it's going into JSON (depending on exactly how you're doing it).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep Simha wrote:Hi All,

How to use actual "\n" character in java? .


Just do clarify a bit: What you're asking for is actually two characters.
 
Hemant B. Kumar
Greenhorn
Posts: 11
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this -

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hemant B. Kumar wrote:Try this -


That will just put a newline in the string. The OP wants the characters \ and n.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!