remove All Multi Space using regex - Java java.lang

Java examples for java.lang:String Whitespace

Description

remove All Multi Space using regex

Demo Code

//package com.java2s;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) {
        String query = "bo  ok 2s.com";
        System.out.println(removeAllMultiSpace(query));
    }/*from   w ww .  ja va2s .  co m*/

    private static final Pattern MULTI_SPACE_ESCAPE = Pattern.compile(" +");

    public static String removeAllMultiSpace(String query) {
        if (query == null || query.length() == 0) {
            return "";
        }
        Matcher matcher = MULTI_SPACE_ESCAPE.matcher(query);
        return matcher.replaceAll(" ");
    }
}

Related Tutorials