Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Greg T Robertson
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.replaceAll() seems a likely candidate
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Greg T Robertson
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
erm, the string could be \\huggy\\bear for all we care, the point is to replace the \\ with \
 
Greg T Robertson
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic