java - 如何删除字符串中的多余空格和新行?

标签 java arrays string

我有一个字符串变量 s,它就像是段落的组合。 例如,

Passages provides funeral and burial products.

Our products are meant to align with your values and bring you comfort.

Our products allow you to offer personalization , flexibility and innovative choices, helping you provide services to a wider range of customers.

我必须创建这种形式的字符串变量:

Passages provides funeral and burial products. Our products are meant to align with your values and bring you comfort. Our products allow you to offer personalization, flexibility and innovative choices, helping you provide services to a wider range of customers.

此外,单词之间(或“.”和单词第一行之间)的多余空格将被删除,并转换为单个空格和“,”,“.”之前的任意数量的空格。或者 ';'将被删除。

我是java新手。谁能告诉我怎么做?

最佳答案

我是 Apache Commons Lang 库的忠实粉丝 - StringUtils 类(具有空安全函数)多年来为我节省了无数时间。毫不奇怪,StringUtils 有一个函数可以满足您的需求:StringUtils.normalizeSpace(String str)

来自 API:

The function returns the argument string with whitespace normalized by using trim(String) to remove leading and trailing whitespace and then replacing sequences of whitespace characters by a single space.

关于java - 如何删除字符串中的多余空格和新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31100733/

相关文章:

python - 如何从 Python 中的字符串中提取字符?

java - 在java中查找模式匹配单词的最快方法

java - 模拟使用 Roo 生成的持久层

C:创建一款游戏,但在某些情况下我遇到了一个错误和无限循环错误

html - WPF 为每个 TextBlock 拆分 HTML 字符串

python - 评估 python 中字符串中子字符串存在的最佳性能方法?

java - ils。从远程服务器调用CURL后重定向不起作用

java - 将一组对象从一个类发送到另一个类

javascript - 如何获取输入字段的值并使用它来过滤数组?

javascript - 如何使用 moment.js/Javascript 列出 2 个时间戳之间的二月月份数?