如何在字符串值中找到回车符?我已经按照方式实现了它,但是对于长字符串值,有时它可能不起作用。谢谢。
String value = "carriage\n return\r\n in\n a\r\n string\n"
for(String line : value.split("\n")) {
if(line.contains("\r")) {
System.out.println("Carriage return found");
}
}
答案 0 :(得分:0)
关注这篇文章:Difference between \n and \r?我认为你可以这样做:
String value = "carriage\n return\r\n in\n a\r\n string\n";
for (String line : value.split("\n|\r"))
if (!line.trim().isEmpty())
System.out.println("Carriage return found:" + line.trim());
输出:
Carriage return found:carriage
Carriage return found:return
Carriage return found:in
Carriage return found:a
Carriage return found:string