views:

1098

answers:

3

I dont want to replace it with /u000A, do NOT want it to look like "asdxyz/u000A"

I want to replace it with the actual newline CHARACTER.

A: 

Use the "(char)(10)" code to generate the true ascii value.

newstr = oldstr.replaceAll("\\n",(char)(10));

// -or-

newstr = oldstr.replaceAll("\\n","" + ((char)(10)));

//(been a while)

Zachery Delafosse
thought this might be it, but it seems to be putting the "\n" into the string, just as if I would do:newstr = oldstr.replaceAll("\\n","\n");
These are both wrong anyway. The the first argument is a regex, and the regex for backslash + 'n' (as a Java String literal) is "\\\\n". As for the replacement, (char)(10) wouldn't compile, and ""+(char)(10) is the same as "\n".
Alan Moore
+5  A: 

Just use String.replace():

newtext = text.replace("\\n", "\n");
Zach Scrivena
+1 since I'm assuming that's what the Q'er wanted ("\n" is already the newline character).
paxdiablo
+5  A: 

Based on your response to Natso's answer, it seems that you have a fundamental misunderstanding of what's going on. The two-character sequence \n isn't the new-line character. But it's the way we represent that character in code because the actual character is hard to see. The compiler knows that when it encounters that two-character sequence in the context of a string literal, it should interpret them as the real new-line character, which has the ASCII value 10.

If you print that two-character sequence to the console, you won't see them. Instead, you'll see the cursor advance to the next line. That's because the compiler has already replaced those two characters with the new-line character, so it's really the new-line character that got sent to the console.

If you have input to your program that contains backslashes and lowercase N's, and you want to convert them to new-line characters, then Zach's answer might be sufficient. But if you want your program to allow real backslashes in the input, then you'll need some way for the input to indicate that a backslash followed by a lowercase N is really supposed to be those two characters. The usual way to do that is to prefix the backslash with another backslash, escaping it. If you use Zach's code in that situation, you may end up turning the three-character sequence \\n into the two-character sequence consisting of a backslash followed by a new-line character.

The sure-fire way to read strings that use backslash escaping is to parse them one character at a time, starting from the beginning of the input. Copy characters from the input to the output, except when you encounter a backslash. In that case, check what the next character is, too. If it's another backslash, copy a single backslash to the output. If it's a lowercase N, then write a new-line character to the output. If it's any other character, so whatever you define to be the right thing. (Examples include rejecting the whole input as erroneous, pretending the backslash wasn't there, and omitting both the backslash and the following character.)

If you're trying to observe the contents of a variable in the debugger, it's possible that the debugger may detect the new-line character convert it back to the two-character sequence \n. So, if you're stepping through your code trying to figure out what's in it, you may fall victim to the debugger's helpfulness. And in most situations, it really is being helpful. Programmers usually want to know exactly what characters are in a string; they're less concerned about how those characters will appear on the screen.

Rob Kennedy
Actually working on a project for a course in compilers and this is part of the lexer. We are working with bantam java. When the lexeeme is matches as a string we want to return the actual value of that string including the special characters /t, /n, /f etc.
In that case, my fourth paragraph is what you need. String.replace will work sometimes, but not often enough for you to get a good grade on your project (at least, not if I were the one grading it).
Rob Kennedy
Yes we were trying to do that exactly, I dont remember why we dropped that idea for replacement. we will revisit it and give another try.