日期:2014-05-20  浏览次数:20607 次

java 删除多余的空格,请不要用指针
例如,str="hello_yyo___koko__!!!";
  newStr="hello_yyo_koko_!!!";



------解决方案--------------------
用replaceAll,当然,还有正则
代码如下:
Java code

public class BlankTest {
    public static void main(String[] args) {
        System.out.println(deleteMulBlank("hello yyo    koko  !!!"));
        System.out.println(deleteMulBlank("afsaf    fdsfdf   fsdfsdf fs fs dfsdfsfsdf sdfsdfsdf sdfsdf sd fs f sdf sdf sdfd"));
    }
    
    public static String deleteMulBlank(String str){
        return str.replaceAll("[ ]{2,}", " ");
    }
}