Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何删除字符串中的多余空格和新行?_Java_Arrays_String - Fatal编程技术网

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

Java 如何删除字符串中的多余空格和新行?,java,arrays,string,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 prov

我有一个字符串变量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的新手。有人能告诉我怎么做吗?

一种方法是逐个字符解析字符串变量。 比如说

StringBuilder sb = new StringBuilder();
String toBeParse = "...";
for (int i = 0; i < toBeParse.length(); i++) {
    if (toBeParse.charAt(i) == condition) {
        sb.append(toBeParse.charAt(i));
    }
}
String result = sb.toString();
试试这个:(@Criti's way)

输出:

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.
试试这个

str = str.replaceAll("\\.\\s+(\\w)", ". $1");

我是Apache Commons Lang库的忠实粉丝,
StringUtils
类(带有空安全函数)多年来为我节省了无数的时间。毫不奇怪,
StringUtils
有一个函数可以完成您所需要的功能:

从API:

函数返回参数字符串,其中空格由 使用trim(String)删除前导和尾随空格,然后 用单个空格替换空格字符序列


string.replaceAll(“\n”,”).replaceAll(“\\s+”,”)

正则表达式的唯一问题是它们可能非常慢。如果您愿意使用外部库,请尝试Google Guava库及其CharMatcher

CharMatcher.WHITESPACE.collapseFrom(“你好,我的名字是Fred,”))


这会将空格转换为单个空格,并将多个空格序列折叠为单个序列。

看看Java中的正则表达式。您的规则似乎不一致。如果使用精确的格式规则编辑问题,您可能会得到一个可用的答案。您可以通过使用条件迭代字符串并复制所有非\n字符或任何其他字符。我已经实现了所有规则,除了如何删除单词与“,”,“.”或“;”之间的所有空格(单词先出现)单词和“.”或“;”之间的空格在您的解决方案中没有被删除。例如:'这很酷。这很热。“不会改成”这很酷。这很热;你能再试一次吗?
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.
str = str.replaceAll("\\.\\s+(\\w)", ". $1");