wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes problem with new line character in the string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with new line character in the string" Watch "problem with new line character in the string" New topic
Author

problem with new line character in the string

Rahul Sud
Greenhorn

Joined: May 27, 2008
Posts: 25
Hi Techies,


I am facing problem in identifying a new line character from a string.

Suppose I have a string lets say =


"RAHUL MAHAJAN" +
"" +
"" +
"RIDHAV SOOD"


In the above string three times the enter key has been used. Now I need to identify this writing a java code which can tell me how many enter key has been used. In short I need to identify the new line character.


Thanks,
Rahul
Moguluri Ravi Kiran
Ranch Hand

Joined: Apr 16, 2010
Posts: 62

"RAHUL MAHAJAN\n\n\nRIDHAV SOOD"

Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 383

just traverse the string content one character at a time and look for '\n'


http://plainoldjavaobject.blogspot.in
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Rahul wrote:In the above string three times the enter key has been used.

Pedantry ON! Not really--you've just added empty strings. If you mean to show that a string contains newlines then you should either use the newline character as shown in a previous response, or format the string using, say, the code tag.This way it's clear what the string you're asking about actually contains, because the Java code you posted originally is just "RAHUL MAHAJANRIDHAVSOOD" :)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
More pedantry. There is not actually a newline character at all; what you have is the LF = linefeed character ((char)0x0a) often written \n. There are several end-of-line characters, the most popular being (char)0x0d or CR for Campbell Ritchie carriage return, often written \r. You may not notice it, but Windows/DOS uses \r\n for end-of-line, *nix and most Macs use \n and older Macs used \r.

Using the simple count of \n works in 99% of cases, but a regular expression for line end sequences may be needed for complete reliability.
Masa Saito
Greenhorn

Joined: Jun 08, 2010
Posts: 14
This is the most platform independent way to handle new lines:

public static final String NEW_LINE = System.getProperty("line.separator");


That techniques asks the OS "hey this is the jvm. how do you start a new line in a string in your environment?" and so no need to hardwire any characters "\n" or "\r" or otherwise.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Masa Saito wrote:This is the most platform independent way to handle new lines:

public static final String NEW_LINE = System.getProperty("line.separator");


That techniques asks the OS "hey this is the jvm. how do you start a new line in a string in your environment?" and so no need to hardwire any characters "\n" or "\r" or otherwise.


Sorry but that is not platform independent. The end-of-line character(s) depend on where (or how) the file is created and not where the file is processed. If I create the file on *nix using a standard *nix editor, transfer in binary mode to Windows then the end-of-line character is not System.getProperty("line.separator"); .

There is no truly platform independent approach but I would use the heretical approach of using a regex looking for "\r\n" or "\r" or "\n" .


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Masa Saito
Greenhorn

Joined: Jun 08, 2010
Posts: 14
James Sabre wrote:
Masa Saito wrote:This is the most platform independent way to handle new lines:

public static final String NEW_LINE = System.getProperty("line.separator");


That techniques asks the OS "hey this is the jvm. how do you start a new line in a string in your environment?" and so no need to hardwire any characters "\n" or "\r" or otherwise.


Sorry but that is not platform independent. The end-of-line character(s) depend on where (or how) the file is created and not where the file is processed. If I create the file on *nix using a standard *nix editor, transfer in binary mode to Windows then the end-of-line character is not System.getProperty("line.separator"); .

There is no truly platform independent approach but I would use the heretical approach of using a regex looking for "\r\n" or "\r" or "\n" .

I see your point... you just helped me find a bug in someone else's source I've been looking at.

But, now that I think about it, this is what I really meant:

and that will always give me the correct newline character (as long as I stay on the same platform). Doing a "\n" hardwire will cause different results in "hello.txt" based on platform.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
If you are writing plain text files, have you considered using a Formatter?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with new line character in the string
 
Similar Threads
Arrays and exceptions!
TextArea and ActionEvent
textarea
If objects can't be passed....
Enter key and JTable