Forums Register Login

Facing Problem with String replaceAll method

+Pie Number of slices to send: Send
Hi,

I have a String variable containing values across multiple lines like -

String str = "Hello World
Welcome To Java
Enjoy coding in Java";

I want to convert it to a single line so that it looks like - "Hello World Welcome To Java Enjoy coding in Java".

I attempted following implementation. But it is not working -

str = str.replaceAll(System.getProperty("line.separator"), " ");

After replaceAll execution, str variable is still containing the original value. Please suggest.

THanks.
+Pie Number of slices to send: Send
Could you please post your code using code tags?
Probably line separator is not what you expect to be
Thank you
+Pie Number of slices to send: Send
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.

Thanks.
+Pie Number of slices to send: Send
I mean what is for you a line separator?
How did you write your string in your code?
Perhaps like the following?



In that case is not
+Pie Number of slices to send: Send
What makes it to work with StringTokenizer and not with replaceAll ?
+Pie Number of slices to send: Send
If you don't post your code i can't answer. I would like to see how you wrote your original string
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 \\.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.

Winston
+Pie Number of slices to send: Send
 

Winston Gutkowski wrote:
str = str.replaceAll("\r?\n", " ");
should work for one from any OS.



Did Mac change its line terminator from \r to \n when it went Unix-based with OS X?
+Pie Number of slices to send: Send
 

Jeff Verdegan wrote:Did Mac change its line terminator from \r to \n when it went Unix-based with OS X?


You know what? I have no idea. Unfortunately, I don't have any Macs lying around to test it with (can't afford 'em ).

Winston
+Pie Number of slices to send: Send
 

Winston Gutkowski wrote:

Jeff Verdegan wrote:Did Mac change its line terminator from \r to \n when it went Unix-based with OS X?


You know what? I have no idea. Unfortunately, I don't have any Macs lying around to test it with (can't afford 'em ).



Then I guess all three cases needs to be taken into account...



Henry
+Pie Number of slices to send: Send
As far as I know, yes Macs did change from \r to \n as a line terminator. What about [\n\r]+ ?

. . . or even ^.+ ?
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:What about [\n\r]+ ?


Doesn't cover "\r\n" (Windows; of course) without allowing through things like "\r\r\n".

Winston
Look ma! I'm selling my stuff!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2910 times.
Similar Threads
use of quotes in type-in qts
strings immutable
Running code written in a string
String replacement
String and StringBuffer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:31:19.