1

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).

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());
}
martin clayton
  • 76,436
  • 32
  • 213
  • 198
oudouz
  • 105
  • 2
  • 3
  • 11

3 Answers3

11

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

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());
    }
Rustam
  • 6,485
  • 1
  • 25
  • 25
  • Rustam, StringBuilder doesn't have a replaceAll method, that's for Strings. And replaceAll uses Regex, I don't want that. Thanks anyway. – oudouz Oct 25 '14 at 18:37
  • As i see your `str` is `String` not `StringBuilder`. – Rustam Oct 25 '14 at 18:40
  • Yeah, but it was the method argument, you don't expect someone to pass a StringBuilder as an argument, I used the String to instantiate an SB object inside the method. – oudouz Oct 25 '14 at 18:43
  • @oudouz hope 2nd answer is what you looking for. – Rustam Oct 25 '14 at 18:56
4

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

kuriouscoder
  • 5,394
  • 7
  • 26
  • 40
1
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.

Prakhar
  • 530
  • 10
  • 24