gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:47:18 25 4
gpt4 key购买 nike

我有一个字符串:

 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com