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