java - 如何在 Android 中将字符串中的双行替换为单行?

标签 java android string replace line-breaks

我有一个字符串:

 This is a test sentence 1.
 This is a test sentence 2.
 This is a test sentence 3.

 This is a test sentence 4.

 This is a test sentence 5.

我想用单行替换双行间隙。如:

 This is a test sentence 1.
 This is a test sentence 2.
 This is a test sentence 3.
 This is a test sentence 4.
 This is a test sentence 5.

但是,我不知道如何检测双换行符,“\n”在替换单换行符时有效,但我在替换函数中尝试了“\n\n”但没有成功。

textView.setText(string.replaceAll("\n\n","\n"));

最佳答案

您可以使用:

str.replaceAll("[\r\n]+", "\n") 

来源:Remove multiple linebreaks

关于java - 如何在 Android 中将字符串中的双行替换为单行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25149579/

相关文章:

r - 根据 r 中的字符串关联数据框中的项目

android - 图标无法正确显示

java - 为什么 InterruptedException 仅从阻塞方法(如 sleep() 和 wait())中抛出?

java - 如何在 Google OR 工具的 VRP 中强制执行硬约束,某些节点不应首先和最后访问

java - “打开方式”对话框

java - Android 应用服务器的行业标准是什么?

android - 如何为安卓手机的音量按钮添加特殊功能?

Python:比较两个应该相同但不相同的字符串

javascript - 如何使用正则表达式用 replaceAll 格式化 JavaScript 字符串

java - 我在这段 Java 代码中做错了什么?