Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Facing Problem with String replaceAll method

 
Hemil Shah
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please post your code using code tags?
Probably line separator is not what you expect to be
Thank you
 
Hemil Shah
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes it to work with StringTokenizer and not with replaceAll ?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What about [\n\r]+ ?

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

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic