This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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.
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: 32694
4
posted
0
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
posted
0
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.
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
posted
0
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: 32694
4
posted
0
If you are writing plain text files, have you considered using a Formatter?