java new line replacement

不想你离开。 提交于 2019-12-01 06:55:42

Strings are immutable. String operations like replaceAll don't modify the instance you call it with, they return new String instances. The solution is to assign the modified string to your original variable.

t = t.replaceAll("\n", "");

Yes, \n is special. It is an escape sequence that stands for a newline. You need to escape it in a string literal in order for it to be actually interpreted the way you want. Append a \ before the sequence so that it looks like this:

"\\n"

Now your program should 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 is coming from somewhere rather than actually being typed by you into the program then you need only add the extra slash in your call to replaceAll()

Edited according to comments.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!