java 多个空格替换成一个空格 一次替换多个空格

将多个空格替换为一个:

string=string.replaceAll(" +"," ");

简单又好用!

必须是replaceAll方法,replace不可以!

/**
 * Created by tiantao on 14-11-27.
 */
public class Test {
    public static void main(String[] args){
        String s = "              ";
        System.out.println("a" + s.replaceAll(" +"," ") + "b");
        System.out.println("a" + s.replace(" +"," ") + "b");
    }
}


你可能感兴趣的:(java 多个空格替换成一个空格 一次替换多个空格)