Java - Write code to Removes any spaces within a string using if and for loop

Requirements

Write code to Removes any spaces within a string.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String s = "b    oo k2s.      com";
        System.out.println(noSpaces(s));
    }//from   ww  w  .j  a  v  a  2s .c o m

    /**
     * Removes any spaces within a string.
     */
    public static String noSpaces(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ' ')
                sb.append(s.charAt(i));
        }

        return (sb.toString());
    }
}

Related Example