我有一个字符串变量 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/