It is line separator only. As the sample string i suggested is getting converted to single line when i use StringTokenizer class. Using StringTokenizer class it has identified the line separator but not with replaceAll method. Below is the code i have used while using StringTokenizer -
StringTokenizer st = new StringTokenizer(str, System.getProperty("line.separator"));
StringBuffer buf = new StringBuffer();
while(st.hasMoreTokens()) {
String token = st.nextToken().trim();
buf.append(token);
}
After this buf contains all in single line. But i am not interested in using StringTokenizer and unable to figure out why it is not working with replaceAll method.
Why are you using a tokenizer? That does the opposite of what is wanted, and is also legacy code which ought not to be .
Sounds like a job for regular expression man! You will find a nice tutorial here. You can use a regular expression for whitespace and use it as one argument for the replace method, and the space as the other argument. You would probably do well to use a quantifier so as not to turn \r\n or \n\n into double space. Remember you may need to escape the backslash, so instead of writing \ you write \\.
Hemil Shah wrote:That is dynamically generated by parsing through XML file. XML file is generated on Unix server and i am parsing it by putting on Windows.
Use this...
instead of using the system property. Unix uses a line feed as the separator, while windows uses the carriage return and line feed combination. This is not normally an issue because the Java println code does a good job and accounting for the difference. The regex stuff doesn't account for this, so it will need to be correct.
Henry Wong wrote:
Use this...instead of using the system property. Unix uses a line feed as the separator, while windows uses the carriage return and line feed combination. This is not normally an issue because the Java println code does a good job and accounting for the difference. The regex stuff doesn't account for this, so it will need to be correct.
Hm, not sure I agree. Java is supposed to be platform-independent, and the above will only work for a string produced by Unix. However
str = str.replaceAll("\r?\n", " "); should work for one from any OS.
The problem with System.getProperty("line.separator") is that it returns the separator for the system being run on, not the one that produced the string.