用< br />替换\ n和\\\\ n在java中 [英] replace \n and \r\n with <br /> in java
本文介绍了用< br />替换\ n和\\\\ n在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.";
我正在尝试更换 \ n
使用< br />
使用
And I'm trying to replace the \n
with <br />
using
str = str.replaceAll("(\r\n|\n)", "<br />");
但 \ n
未获得更换。
我尝试使用此 RegEx工具进行验证,我看到相同的结果。输入字符串与(\\\\ n | \ n)
不匹配。我做错了什么?
but the \n
is not getting replaced.
I tried to use this RegEx Tool to verify and I see the same result. The input string does not have a match for "(\r\n|\n)"
. What am i doing wrong ?
推荐答案
它对我有用。
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);
}
}
结果:
This is a string.<br />This is a long string.
您的问题出在其他地方。
Your problem is somewhere else.
这篇关于用< br />替换\ n和\\\\ n在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文