将所有连续的"\ r \ n"或"\ n"替换为一个< br> [英] Replacing all consecutive "\r\n or \n"s with one single <br>
本文介绍了将所有连续的"\ r \ n"或"\ n"替换为一个< br>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我仅显示使用以下两种替换方法从数据库获得的消息:
Currently I am showing messages that I get from DB using only these two replacements:
text = text.replace("\r\n", "<br />");
text = text.replace("\n", "<br />");
,但是问题是,如果有很多连续的"\ n",我将有很多
或空格,我只想将它们全部变成一个.那么您的建议是什么?是否有一种快速替换方法,以使所有连续\ n \ n \ n \ n \ n \ n \ n仅一个而只有一个br?
but the problem is if there are many consecutive "\n"s I will have lots of
s or white spaces, I just want to make them all one. So what is your suggestion? Is there a quick replace method to make all consecutive \n\n\n\n\n\n\n only one just one br?
推荐答案
您可以使用量词+
表示1或更多.
另外,*
表示0或更大.
You can use quantifier +
to denote 1 or more..
Also, *
means 0 or more..
text = text.replaceAll("\n+", "<br />");
text = text.replaceAll("[\n\r]+", "<br />");
这篇关于将所有连续的"\ r \ n"或"\ n"替换为一个< br>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文