Java - Write code to Remove extra (more than one) whitespace characters from any place of the string.

Requirements

Write code to Remove extra (more than one) whitespace characters from any place of the string.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com  ";
        System.out.println(removeExtraSpaces(str));
    }/*from  ww w .  j  a va 2  s . co  m*/

    /**
     * Removes extra (more than one) whitespace characters from any place of the string.<br>
     * Examples:<br>
     * " aaa  bbb   ccc ddd " becomes "aaa bbb ccc ddd"
     *
     */
    public static String removeExtraSpaces(String str) {
        StringBuilder sb = new StringBuilder();

        int pos = 0;
        boolean prevWS = true;

        for (int i = 0; i < str.length(); i++) {
            if (Character.isWhitespace(str.charAt(i))
                    || i == str.length() - 1) {
                if (!prevWS) {
                    sb.append(str.substring(pos, i)).append(str.charAt(i));
                }
                prevWS = true;
            } else {
                if (prevWS)
                    pos = i;
                prevWS = false;
            }
        }
        if (Character.isWhitespace(sb.charAt(sb.length() - 1)))
            sb.deleteCharAt(sb.length() - 1);

        return sb.toString();
    }
}