aspose file tools*
The moose likes Beginning Java and the fly likes Facing Problem with String replaceAll method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Facing Problem with String replaceAll method" Watch "Facing Problem with String replaceAll method" New topic
Author

Facing Problem with String replaceAll method

Hemil Shah
Greenhorn

Joined: Feb 18, 2010
Posts: 8
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.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Could you please post your code using code tags?
Probably line separator is not what you expect to be
Thank you


Bye,
Nicola
Hemil Shah
Greenhorn

Joined: Feb 18, 2010
Posts: 8
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.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
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
Hemil Shah
Greenhorn

Joined: Feb 18, 2010
Posts: 8
What makes it to work with StringTokenizer and not with replaceAll ?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
If you don't post your code i can't answer. I would like to see how you wrote your original string
Hemil Shah
Greenhorn

Joined: Feb 18, 2010
Posts: 8
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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 \\.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7838
    
  21

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
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7838
    
  21

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
As far as I know, yes Macs did change from \r to \n as a line terminator. What about [\n\r]+ ?

. . . or even ^.+ ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7838
    
  21

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

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

Winston
 
wood burning stoves
 
subject: Facing Problem with String replaceAll method