Java - Write code to replace Blank using regex

Requirements

Write code to replace Blank using regex

Demo

//package com.book2s;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) {
        String str = "b  oo  k2s.com";
        System.out.println(replaceBlank(str));
    }/*  ww w.  j a va2 s . c om*/

    public static String replaceBlank(String str) {
        //Pattern p = Pattern.compile("\\s*|\t|\r|\n"); 
        Pattern p = Pattern.compile("\\s|,|-|_*");
        Matcher m = p.matcher(str);
        String afterStr = m.replaceAll("");
        return afterStr;
    }

    public static String replaceAll(String src, String replace, String dest) {
        StringBuffer buf = new StringBuffer(src);
        String m_dest = "";

        if (dest != null) {
            m_dest = dest;
        }

        int i = 0;

        while ((i = buf.indexOf(replace, i)) != -1) {
            buf = new StringBuffer(buf.subSequence(0, i) + m_dest
                    + buf.substring(i + replace.length()));
            i += m_dest.length();
        }
        return buf.toString();
    }
}

Related Exercise