aspose file tools*
The moose likes Java in General and the fly likes combining characters - \\r\\n to \r\n Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "combining characters - \\r\\n to \r\n" Watch "combining characters - \\r\\n to \r\n" New topic
Author

combining characters - \\r\\n to \r\n

Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
Let's say I have a String as such \\r\\n - and I really want that String to be \r\n - how can I programmatically combine the characters to build the String so that the original \\ is replaced with a "\" + next character? e.g. making the string into "\r\n"
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
String.replaceAll() seems a likely candidate
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Just make sure you assign the return value, a String, to another String as String objects are immutable. (You probably know that but maybe some newbies reading these posts for tips may not.)


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
Knew about the String being immutable and thanks for the input. Regex is not what one would call my strong suit. Is there a way to do a single regex that says if ('\\') then replace with '\ + next character' ???
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
I came up with the following:

output: \n\r

There a several layers of 'escapism' going on.

For string \\\\ will end up as the string literal \\
on the replaceAll, \\\\\\\\ is the string literal \\\\, which is further escaped during the regex as \\, the 'same' being true for the replace value.
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
But the output should really be \n\r (e.g. a carriage return line feed). The original String was \\r\\n not \\\r\\\n ....

For now I've resorted to using System.getProperty("line.separator"); as opposted to trying to use my own property ...
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
erm, the string could be \\huggy\\bear for all we care, the point is to replace the \\ with \
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
Perhaps I wasn't clear - what I want to do is change \\r\\n to \r\n - exactly like that - so if my current string contains 4 characters the escaped slash, r, escaped slash, and n - I want it to be come the escaped r, and escaped n.



and the output is \\n\\r
\n\r


Looking at the output it doesn't appear to be creating the special characters for \n or \r rather just removing one of the slashes.

Not sure it's the same thing ....
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Yep, you're right. No special characters are being created. The solution is similar though:

output:
Hello
World

I'm just guessing though...I've assumed that you're reading \\r\\n in from a file such that you end up with the actual string: \\r\\n.

If, instead, this is a string you've defined in your program as \\r\\n, then it's really running around as the literal, non-special characters \r\n. Now, if that's what you're doing, I have to wonder why?

If you want a line return to append to (or insert in) your lines, it would be much easier to do something like:

String endline = System.getProperty( "line.separator" );

and use endline where ever you need it.
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
you are correct - it's coming from a file - as a property trying to set - we have moved to the System.getProperty("line.separator") at this point (yesterday sometime) I was still trying to determine if there was another approach to it. Thanks for your help.
 
Consider Paul's rocket mass heater.
 
subject: combining characters - \\r\\n to \r\n