java 给定一个单词之间有多个空格的句子。删除多余的空格,使句子在单词之间只有一个空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26565705/
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-11-02 10:09:15  来源:igfitidea点击:

Given a sentence with multiple spaces between words. Remove the extra spaces so that the sentence will have exactly one space between words

javastringwhitespacespace

提问by oudouz

The question is clear. Code should be in Java and without using Regex (In case someone didn't notice, that's not a duplicate, I'm asking for a way to do it without regex).

这个问题很清楚。代码应该是 Java 并且不使用正则表达式(如果有人没有注意到,那不是重复的,我要求一种没有正则表达式的方法)。

input: This is  a string     with more than   one space between words.

output: This is a string with more than one space between words.

Is there a better way than doing it this way ?

有没有比这样做更好的方法?

public static String delSpaces(String str){
    StringBuilder sb = new StringBuilder(str);
    ArrayList<Integer> spaceIndexes = new ArrayList<>();

    for ( int i=0; i < sb.length(); i++ ){
        if ( sb.charAt(i) == ' ' && sb.charAt(i-1) == ' '){
            spaceIndexes.add(i);
        }
    }

    for (int i = 0; i < spaceIndexes.size(); i++){
        sb.deleteCharAt(spaceIndexes.get(i)-i);
    }
    return new String(sb.toString());
}

回答by Rustam

use str.replaceAll("\\s+"," ");// simplest way using regular expression

use str.replaceAll("\\s+"," ");// 使用正则表达式的最简单方法

2nd way :

第二种方式:

public static String delSpaces(String str){    //custom method to remove multiple space
        StringBuilder sb=new StringBuilder();
        for(String s: str.split(" ")){

            if(!s.equals(""))        // ignore space
             sb.append(s+" ");       // add word with 1 space

        }
        return new String(sb.toString());
    }

3rd way :

第三种方式:

public static String delSpaces(String str){
        int space=0;
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)!=' '){
                sb.append(str.charAt(i));  // add character
                space=0;
            }else{
                space++;
                if(space==1){       // add 1st space
                    sb.append(" ");
                }
            }
        }
        return new String(sb.toString());
    }

回答by kuriouscoder

str = str.replaceAll("\\s+", " ");would do the trick with regex. This is much faster than you having to write a method to dos.

str = str.replaceAll("\\s+", " ");会用正则表达式来解决问题。这比您必须为 dos 编写方法要快得多。

回答by Prakhar

class Try 
{
    public static void main(String args[])
    {
        String str = "This is  a string     with more than   one space between words.";
        char[] c = str.toCharArray();
        String str1 = "";
        for(int i = 0;i<str.length()-1;i++)
        {
            if((c[i] == ' '&& c[i+1] != ' ') || (c[i] != ' '))
                str1 += c[i];
        }

        System.out.println(str1);
    }
}

This works easily.

这很容易工作。