Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings with \n newline chars

 
Roseanna Jeanette
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method which is returning a String. Within the method it builds this string up by adding bits to it e.g. returnString = returnString + "XXXX" +"\n";
This works fine and within the method I can print it out in full. However when I assign this value to the return value string and try and print it from outside the method, it only prints one line of the string. I have tried instead making the method return an array of Strings but that complicates it for other parts of the program so I really want to keep it as a String. Any ideas on how I can get the entirety of the String to be printed on multiple lines from outside the method?
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please post your code?
 
Winston Gutkowski
Bartender
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roseanna Jeanette wrote:Any ideas on how I can get the entirety of the String to be printed on multiple lines from outside the method?

Yes. Don't use anything created by Microsoft (including the command window) to display it.

I suspect very strongly that what you're running into is the "MS experience" when it comes to text lines, because Windows (alone among desktop systems these days) uses the old CRLF style of ending a line.

However, Swastik is quite right. A bit of code would help us out a lot.

Winston
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:I suspect very strongly that what you're running into is the "MS experience" when it comes to text lines, because Windows (alone among desktop systems these days) uses the old CRLF style of ending a line.


I know it is fun to spread Microsoft hate and blame them for everything, but using '\n' as a line separator is not what you are supposed to use - you are supposed to use System.lineSeparator() to be platform independent. Blaming MS for not being unix-based is not a solution.
 
Junilu Lacar
Bartender
Posts: 7482
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java versions prior to Java SE 7, you have to use System.getProperty("line.separator")
 
Winston Gutkowski
Bartender
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:I know it is fun to spread Microsoft hate and blame them for everything, but using '\n' as a line separator is not what you are supposed to use - you are supposed to use System.lineSeparator() to be platform independent. Blaming MS for not being unix-based is not a solution.

Actually, I have to disagree there. Since we (still) know nothing of what Roseanna was trying to do - or even if she wrote the code herself - the only thing I can go on is the problem she described: not being able to see lines containing newlines displayed correctly.

Furthermore, this is a classic example of the reason for "Microsoft hate". They've had 30 years to fix the problem and do things the way that every other major desktop and intermediate system on the planet does (even Apple now), but they've chosen to ignore it.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I learned CRLF as the way to terminate a line long before Microsoft was founded.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Actually I learned CRLF as the way to terminate a line long before Microsoft was founded.


Me too. On a typewriter. The carriage return lever pivoting on its hinge engaged gears that gave us the LF, and the return of the platen (carriage) back to the right-hand end of its travel gave us the CR (Although to be honest, MS may have existed at the point I learned this. I don't actually know when they were founded.)
 
Winston Gutkowski
Bartender
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Actually I learned CRLF as the way to terminate a line long before Microsoft was founded.

Me three. But if it's for the same reason as Jeff, or because you remember line-printers, don't you think we should have got past this stupid anomaly - perpetuated only by the "hated" - by 2013?

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic