Java - Write code to delete Whitespace from a string

Requirements

Write code to delete Whitespace from a string

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "bo    ok  2s.com";
        System.out.println(deleteWhitespace(str));
    }/*  w  ww.j a  va 2 s  .  c  om*/

    public static String deleteWhitespace(String str) {
        if (str == null) {
            return null;
        }

        int sz = str.length();
        StringBuffer buffer = new StringBuffer(sz);

        for (int i = 0; i < sz; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                buffer.append(str.charAt(i));
            }
        }

        return buffer.toString();
    }

}

Related Exercise