Replacing a new java line
I am wondering why I am not getting the expected result with this:
String t = "1302248663033 <script language='javascript'>nvieor\ngnroeignrieogi</script>"; t.replaceAll("\n", ""); System.out.println(t); Output:
1302248663033 <script language='javascript'>nvieor gnroeignrieogi</script> So I wonder why \n still exists. Somebody knows? Is \ n special?
EDIT:
I'm having trouble matching a newline with. in a regex expression, not understanding what to use the DOTALL function, so I'll add what needs to be done here for future reference:
String text = null; text = FileUtils.readFileToString(inFile); Pattern p = Pattern.compile("<script language='javascript'>.+?</script>\n", Pattern.DOTALL); text = p.matcher(text).replaceAll(""); out.write(text); Yes, \n is special. This is an escape sequence that denotes a new line. You need to avoid this in a string literal so that it is actually interpreted the way you want. Add \ in front of the sequence so that it looks like this:
"\\n" Your program should now look like this:
String t = "1302248663033 <script language='javascript'>nvieor\\ngnroeignrieogi</script>"; t = t.replaceAll("\\n", ""); System.out.println(t); Of course, if the string t comes from somewhere, and is not typed by you into the program, you need to add an additional slash to your replaceAll() call
Edited in accordance with the comments.