如何使用正则表达式或其他方法删除java字符串中的额外“\r\n”
我有这样一个字符串:
String orginal = "this is for test hello\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n" +
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nhello" +
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nhello";
在文本区域中输入内容时,用户经常按“enter”,因此在java中,它会更改为大量的“\r\n”。 我想将其更改为:
String newStr = "this is for test hello\r\nhello\r\nhello"
我怎样才能只保留一个\r\n
# 1 楼答案
这应该可以做到
您可能还想考虑使用^ {CD1>}系统属性而不是硬编码^ {CD2>},因为这可能在其他平台上有所不同。例如,Unix只使用单行提要
\n
。这会更便于携带# 2 楼答案
通用解决方案(LF-UNIX、CR-Mac、CRLF-Windows)可能是这样的