Java String.replaceAll()方法

 声明

    以下是java.lang.String.replaceAll()方法的声明

   public String replaceAll(String regex, String replacement)

参数:
  regex 正则表达式,
replacement 用来替换符合正则表达式的字符符

 返回值

   此方法返回的结果字符串

 异常

 PatternSyntaxException -- 如果正则表达式的语法无效.


用例
  /**  
     * 使用java正则表达式去掉多余的.与0 (1.0->1,1.010->1.01,3-3)
     * @param s  
     * @date 2016-8-29 
     * @author yk
     * @return   
     */    
    public static String subZeroAndDot(String toDot){    
        if(toDot.indexOf(".") > 0){    
            //去掉多余的0    
            toDot = toDot.replaceAll("0+?$", "");
            //如最后一位是.则去掉    
            toDot = toDot.replaceAll("[.]$", "");
        }    
        return toDot;    
    } 





posted @ 2016-09-06 19:31  杀出重围啊  阅读(2413)  评论(0编辑  收藏  举报