【问题标题】:Given a sentence with multiple spaces between words. Remove the extra spaces so that the sentence will have exactly one space between words [duplicate]给定一个单词之间有多个空格的句子。删除多余的空格,使句子在单词之间只有一个空格[重复]
【发布时间】:2014-12-21 08:11:43
【问题描述】:

问题很明确。代码应该是 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.

有没有比这种方式更好的方法?

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());
}

【问题讨论】:

    标签: java string whitespace space


    【解决方案1】:

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

    第二种方式:

    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());
        }
    

    第三种方式:

    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());
        }
    

    【讨论】:

    • Rustam,StringBuilder 没有用于字符串的 replaceAll 方法。而replaceAll 使用正则表达式,我不希望这样。无论如何,谢谢。
    • 我看到你的 strString 而不是 StringBuilder
    • 是的,但它是方法参数,你不要指望有人将StringBuilder作为参数传递,我使用String在方法内部实例化了一个SB对象。
    • @oudouz 检查更新代码
    • @oudouz 希望第二个答案是您想要的。
    【解决方案2】:

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

    【讨论】:

    • 我知道它更快,但我想要一个算法。我想写我自己的方法。
    【解决方案3】:
    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);
        }
    }
    

    这很容易。

    【讨论】:

    • String.replaceAll() 使用正则表达式作为第一个参数 Prakhar。
    • oudouz,这可能会有所帮助
    • @Prakhar 避免在循环内向String 变量添加字符,因为每次都会创建新的String。最好使用StringBuilderStringBuffer
    • 是的,真的..谢谢(y)
    猜你喜欢
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多