I'm trying to replace all sequences of two or more whitespaces in a string with a single space.
I've already got it using the String.trim().replaceAll(" +", " ") method.
But I was trying to figure it out on my own using what I knew, and I got close, wondering if anyone can fix what I have to make it work.
Here's what I was trying:
StringBuilder sampleTextBuilder = new StringBuilder(sampleText);
for (int i = 0; i < sampleTextBuilder.length(); i++) {
if (Character.isLetter(sampleTextBuilder.charAt(i))) {
isLetter = true;
isFirstWS = false;
}
else if (Character.isWhitespace(sampleTextBuilder.charAt(i)) && isLetter) {
isFirstWS = true;
isLetter = false;
}
else if (Character.isWhitespace(sampleTextBuilder.charAt(i)) && isFirstWS) {
sampleTextBuilder.deleteCharAt(i);
}
sampleText = sampleTextBuilder.toString();
}