Will return string without redundant spaces - Java java.lang

Java examples for java.lang:String Search

Description

Will return string without redundant spaces

Demo Code


//package com.java2s;

public class Main {
    /** Will return string without redundant spaces,
     * that means string 'str' will be trimmed
     * and all 'groups-of-more-than-one-spaces'(if any)
     * will be replaced by exactly one space.
     * @param str string where you want to remove redundant spaces
     * @return trimmed string with only space between each group of chars(if any)
     *//*from  w ww .  j ava  2  s . com*/
    public static String removeRedundantSpaces(String str) {
        return str.trim().replaceAll("\\s+", "");
    }
}

Related Tutorials