将多个空格替换为一个:
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");
}
}