如何使用Java删除字符串中的重复空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3958955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:34:45  来源:igfitidea点击:

How to remove duplicate white spaces in string using Java?

javastringwhitespace

提问by brainless

How to remove duplicate white spaces (including tabs, newlines, spaces, etc...) in a string using Java?

如何使用 Java 删除字符串中的重复空格(包括制表符、换行符、空格等)?

采纳答案by aioobe

Like this:

像这样:

yourString = yourString.replaceAll("\s+", " ");

For example

例如

System.out.println("lorem  ipsum   dolor \n sit.".replaceAll("\s+", " "));

outputs

产出

lorem ipsum dolor sit.


What does that \s+mean?

这是什么\s+意思?

\s+is a regular expression. \smatches a space, tab, new line, carriage return, form feed or vertical tab, and +says "one or more of those". Thus the above code will collapse all "whitespace substrings" longer than one character, with a single space character.

\s+是一个正则表达式。\s匹配空格、制表符、换行符、回车、换页或垂直制表符,并+表示“其中一个或多个”。因此,上面的代码将折叠所有长度超过一个字符的“空白子字符串”,并使用一个空格字符。



Source: Java: Removing duplicate white spaces in strings

来源:Java:删除字符串中的重复空格

回答by Sachin Shanbhag

Try this - You have to import java.util.regex.*;

试试这个 - 你必须 import java.util.regex.*;

    Pattern pattern = Pattern.compile("\s+");
    Matcher matcher = pattern.matcher(string);
    boolean check = matcher.find();
    String str = matcher.replaceAll(" ");

Where stringis your string on which you need to remove duplicate white spaces

string您需要删除重复空格的字符串在哪里

回答by codaddict

You can use the regex

您可以使用正则表达式

(\s)

and

replace it with $1.

将其替换为$1.

Java code:

爪哇代码:

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

If the input is "foo\t\tbar "you'll get "foo\tbar "as output
But if the input is "foo\t bar"it will remain unchanged because it does not have any consecutive whitespace characters.

如果输入是,"foo\t\tbar "您将获得"foo\tbar "输出
但是如果输入是"foo\t bar",它将保持不变,因为它没有任何连续的空白字符。

If you treat all the whitespace characters(space, vertical tab, horizontal tab, carriage return, form feed, new line) as space then you can use the following regex to replace anynumber of consecutive white space with a single space:

如果您将所有空格字符(空格、垂直制表符、水平制表符、回车、换页、换行)视为空格,那么您可以使用以下正则表达式将任意数量的连续空格替换为单个空格:

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

But if you want to replace two consecutive white space with a single space you should do:

但是如果你想用一个空格替换两个连续的空格,你应该这样做:

str = str.replaceAll("\s{2}"," ");

回答by Dheeraj at Techreuters

This can be possible in three steps:

这可以通过三个步骤来实现:

  1. Convert the string in to character array (ToCharArray)
  2. Apply for loop on charater array
  3. Then apply string replace function (Replace ("sting you want to replace"," original string"));
  1. 将字符串转换为字符数组 (ToCharArray)
  2. 在字符数组上应用 for 循环
  3. 然后应用字符串替换函数(Replace("sting you want to replace"," original string"));

回答by jonnysamps

If you want to get rid of all leading and trailing extraneous whitespace then you want to do something like this:

如果你想摆脱所有前导和尾随无关的空白,那么你想要做这样的事情:

// \A = Start of input boundary
// \z = End of input boundary 
string = string.replaceAll("\A\s+(.*?)\s+\z", "");

Then you can remove the duplicates using the other strategies listed here:

然后,您可以使用此处列出的其他策略删除重复项:

string = string.replaceAll("\s+"," ");

回答by wutzebaer

hi the fastest (but not prettiest way) i found is

嗨,我发现的最快(但不是最漂亮的方式)是

while (cleantext.indexOf("  ") != -1)
  cleantext = StringUtils.replace(cleantext, "  ", " ");

this is running pretty fast on android in opposite to an regex

与正则表达式相反,这在 android 上运行得非常快

回答by arnobpl

Though it is too late, I have found a better solution (that works for me) that will replace all consecutive same type white spaces with one white space of its type. That is:

虽然为时已晚,但我找到了一个更好的解决方案(对我有用),它将用一个相同类型的空格替换所有连续的相同类型的空格。那是:

   Hello!\n\n\nMy    World  

will be

将会

 Hello!\nMy World 

Notice there are still leading and trailing white spaces. So my complete solution is:

请注意,仍然存在前导和尾随空格。所以我的完整解决方案是:

str = str.trim().replaceAll("(\s)+", ""));

Here, trim()replaces all leading and trailing white space strings with "". (\\s)is for capturing \\s(that is white spaces such as ' ', '\n', '\t') in group #1. +sign is for matching 1 or more preceding token. So (\\s)+can be consecutive characters (1 or more) among any single white space characters (' ', '\n' or '\t'). $1is for replacing the matching strings with the group #1string (which only contains 1 white space character) of the matching type (that is the single white space character which has matched). The above solution will change like this:

在这里,trim()用“”替换所有前导和尾随空格字符串。(\\s)用于捕获组 #1 中的\\s(即空格,例如 ' ', '\n', '\t')。符号用于匹配 1 个或多个前面的标记。因此可以是任何单个空白字符(' '、'\n' 或 '\t')中的连续字符(1 个或多个)。用于用匹配类型(即匹配的单个空格字符)的组 #1字符串(仅包含 1 个空格字符)替换匹配字符串。上面的解决方案会变成这样:+(\\s)+$1

   Hello!\n\n\nMy    World  

will be

将会

Hello!\nMy World

I have not found my above solution here so I have posted it.

我还没有在这里找到我的上述解决方案,所以我发布了它。

回答by imvp

You can also try using String Tokeniser, for any space, tab, newline, and all. A simple way is,

您还可以尝试对任何空格、制表符、换行符等使用 String Tokeniser。一个简单的方法是,

String s = "Your Text Here";        
StringTokenizer st = new StringTokenizer( s, " " );
while(st.hasMoreTokens())
{
    System.out.print(st.nextToken());
}

回答by oleg.cherednik

String str = "   Text    with    multiple    spaces    ";
str = org.apache.commons.lang3.StringUtils.normalizeSpace(str);
// str = "Text with multiple spaces"