我的代码是:
String str = str.replaceAll("\\n\\n","\\n");
我要做的是用1个新行替换两个连续的新行。我的代码没有这样做。
人们在关闭问题后编辑:
我正在Windows上运行代码。
我想要做的是替换:"This is the first line.\nThis is the second one\nThis is the third one\n\n\This is the fourth one"
使用:"This is the first line.\nThis is the second one\nThis is the third one\nThis is the fourth one"
在那里你看到在第三行后我想要一个换行而不是两行换行。
答案 0 :(得分:3)
System.out.println(s.replaceAll("\n+","\n")); \\ replace one or more newline characters with just one newline..
示例:
public static void main(String[] args) {
String s = "abc\n\nabc";
System.out.println(s);
System.out.println("hello");
System.out.println(s.replaceAll("\n+", "\n"));
}
O/P:
abc
abc
hello
abc
abc
答案 1 :(得分:0)
String str = str.replaceAll("\n\n","\n");
试试这个