正则表达式对字符串的常见操作_替换

其实使用的就是String类中的replaceAll()方法。

public class RegexDemo {
    public static void main(String[] args) {

        functionDemo();
    }
    public static void functionDemo() {
        
        String str = "zhangsanttttxiaoqiangmmmmmmzhaoliu";
        
        str = str.replaceAll("(.)\\1+", "$1");
        
        System.out.println(str);
        
        String tel = "15800001111";//158****1111;
        
        tel = tel.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        
        System.out.println(tel);
    }
}

 

posted @ 2014-03-16 15:35  胡椒粉hjf  阅读(202)  评论(0)    收藏  举报