[英]replace \n and \r\n with <br /> in java
This has been asked several times for several languages but I can't get it to work. 有几种语言曾多次询问过这种情况,但我无法使用它。 I have a string like this
我有这样的字符串
String str = "This is a string.\nThis is a long string.";
And I'm trying to replace the \\n
with <br />
using 而我正试图用
<br />
取代\\n
str = str.replaceAll("(\r\n|\n)", "<br />");
but the \\n
is not getting replaced. 但是
\\n
没有被取代。 I tried to use this RegEx Tool to verify and I see the same result. 我尝试使用此RegEx工具进行验证,我看到相同的结果。 The input string does not have a match for
"(\\r\\n|\\n)"
. 输入字符串与
"(\\r\\n|\\n)"
不匹配。 What am i doing wrong ? 我究竟做错了什么 ?
It works for me. 这个对我有用。
public class Program
{
public static void main(String[] args) {
String str = "This is a string.\nThis is a long string.";
str = str.replaceAll("(\r\n|\n)", "<br />");
System.out.println(str);
}
}
Result: 结果:
This is a string.<br />This is a long string.
Your problem is somewhere else. 你的问题在别的地方。
你正在尝试的更强大的版本:
str = str.replaceAll("(\r\n|\n\r|\r|\n)", "<br />");
For me, this worked: 对我来说,这工作:
rawText.replaceAll("(\\\\r\\\\n|\\\\n)", "\\\n");
Tip: use regex tester for quick testing without compiling in your environment 提示:使用正则表达式测试程序进行快速测试,无需在您的环境中进行编译
Since my account is new I can't up-vote Nino van Hooff's answer. 由于我的帐户是新的,我无法对Nino van Hooff的回答进行投票。 If your strings are coming from a Windows based source such as an aspx based server, this solution does work:
如果您的字符串来自基于Windows的源,例如基于aspx的服务器,则此解决方案可以正常工作:
rawText.replaceAll("(\\\\r\\\\n|\\\\n)", "<br />");
Seems to be a weird character set issue as the double back-slashes are being interpreted as single slash escape characters. 似乎是一个奇怪的字符集问题,因为双反斜杠被解释为单斜杠转义字符。 Hence the need for the quadruple slashes above.
因此需要上面的四倍斜线。
Again, under most circumstances "(\\\\r\\\\n|\\\\n)"
should work, but if your strings are coming from a Windows based source try the above. 同样,在大多数情况下
"(\\\\r\\\\n|\\\\n)"
应该可以正常工作,但如果您的字符串来自基于Windows的源,请尝试以上操作。
Just an FYI tried everything to correct the issue I was having replacing those line endings. 只是一个FYI尝试了一切来纠正我正在替换那些行结尾的问题。 Thought at first was failed conversion from
Windows-1252
to UTF-8
. 首先想到的是从
Windows-1252
到UTF-8
转换失败。 But that didn't working either. 但那也没有奏效。 This solution is what finally did the trick.
这个解决方案终于成功了。 :)
:)
It works for me. 这个对我有用。 The Java code works exactly as you wrote it.
Java代码与您编写的完全一样。 In the tester, the input string should be:
在测试器中,输入字符串应为:
This is a string.
This is a long string.
...with a real linefeed. ......带有真正的换行符。 You can't use:
你不能使用:
This is a string.\nThis is a long string.
...because it treats \\n
as the literal sequence backslash 'n' . ...因为它将
\\n
视为字面序列反斜杠'n' 。
That should work, but don't kill yourself trying to figure it out. 这应该有效,但不要试图弄明白自己。 Just use 2 passes.
只需使用2次传球。
str = str.replaceAll("(\r\n)", "<br />");
str = str.replaceAll("(\n)", "<br />");
Disclaimer: this is not very efficient. 免责声明:这不是很有效。
This should work. 这应该工作。 You need to put in two slashes
你需要加两个斜杠
str = str.replaceAll("(\\r\\n|\\n)", "<br />");
In this Reference , there is an example which shows 在本参考文献中 ,有一个例子显示
private final String REGEX = "\\d"; // a single digit
I have used two slashes in many of my projects and it seems to work fine! 我在我的许多项目中使用了两个斜线,它似乎工作正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.