replace \n and \r\n with
in java

后端 未结 7 1429
余生分开走
余生分开走 2020-11-29 04:49

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 l         


        
相关标签:
7条回答
  • 2020-11-29 05:47

    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:

    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.

    Just an FYI tried everything to correct the issue I was having replacing those line endings. Thought at first was failed conversion from Windows-1252 to UTF-8. But that didn't working either. This solution is what finally did the trick. :)

    0 讨论(0)
提交回复
热议问题