Java +如何在字符串值中查找回车符

时间:2015-05-28 09:02:06

标签: java string carriage-return

如何在字符串值中找到回车符?我已经按照方式实现了它,但是对于长字符串值,有时它可能不起作用。谢谢。

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");
    }
}

1 个答案:

答案 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