在java中用<br />替换\n和\r\n

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3056834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:03:29  来源:igfitidea点击:

replace \n and \r\n with <br /> in java

javaregexreplaceall

提问by Bala R

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 \nwith <br />using

我试图\n<br />使用来替换

str = str.replaceAll("(\r\n|\n)", "<br />");

but the \nis not getting replaced. I tried to use this RegEx Toolto verify and I see the same result. The input string does not have a match for "(\r\n|\n)". What am i doing wrong ?

\n没有被取代。我尝试使用此RegEx 工具进行验证,但看到了相同的结果。输入字符串不匹配"(\r\n|\n)"。我究竟做错了什么 ?

采纳答案by Mark Byers

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.

你的问题在别处。

回答by Byron Whitlock

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.

免责声明:这不是很有效。

回答by Kasturi

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!

我在我的许多项目中使用了两个斜线,它似乎工作正常!

回答by Dolph

A little more robust version of what you're attempting:

您正在尝试的更强大的版本:

str = str.replaceAll("(\r\n|\n\r|\r|\n)", "<br />");

回答by Alan Moore

It works for me. The Java code works exactly as you wrote it. In the tester, the input string should be:

这个对我有用。Java 代码完全按照您编写的方式工作。在测试器中,输入字符串应该是:

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 \nas the literal sequence backslash 'n'.

...因为它被视为\n文字序列反斜杠 'n'

回答by Nino van Hooff

For me, this worked:

对我来说,这有效:

rawText.replaceAll("(\\r\\n|\\n)", "\\n");

Tip: use regex testerfor quick testing without compiling in your environment

提示:使用regex tester进行快速测试,无需在您的环境中编译

回答by user3798668

Since my account is new I can't up-vote Nino van Hooff's answer. If your strings are coming from a Windows based source such as an aspx based server, this solution does work:

由于我的帐户是新帐户,因此我无法对 Nino van Hooff 的回答进行投票。如果您的字符串来自基于 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. Thought at first was failed conversion from Windows-1252to UTF-8. But that didn't working either. This solution is what finally did the trick. :)

仅供参考,我尝试了一切来纠正我替换这些行尾的问题。起初以为是从Windows-1252to转换失败UTF-8。但这也不起作用。这个解决方案最终成功了。:)