Java - Write code to Replace all whitespaces and non visible characters such as tab, \n with given string token.

Requirements

Write code to Replace all whitespaces and non visible characters such as tab, \n with given string token.

Demo

//package com.book2s;

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

    /**
     * Replace all whitespaces and non visible characters such as tab, \n
     * with given string token.
     * 
     * @param str
     * @return String
     */
    public static String replaceAllSpace(String str, String replacement) {
        return str.replaceAll("\\s+", replacement);
    }
}

Related Exercise