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"
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' ???
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.
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.
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:
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.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop