EDIT: leaving this answer to posterity but Keith Randall O (n) solution is much nicer.
You may find it more efficient to work from the far end - this way, by the time you delete the earlier characters, you won't copy the spaces later.
, , delete, deleteCharAt. - :
private static StringBuilder removeBlankSpace(StringBuilder sb) {
int currentEnd = -1;
for(int i = sb.length() - 1; i >= 0; i--) {
if (Character.isWhitespace(sb.charAt(i))) {
if (currentEnd == -1) {
currentEnd = i + 1;
}
} else {
if (currentEnd != -1) {
sb.delete(i + 1, currentEnd);
currentEnd = -1;
}
}
}
if (currentEnd != -1) {
sb.delete(0, currentEnd);
}
return sb;
}