Java 简单实用方法二
整理以前的笔记,在学习Java时候,经常会用到一些方法。虽然简单但是经常使用。因此做成笔记,方便以后查阅  
这篇博文先说明构造和使用这些方法。
1,判断String类型数据是否包含中文
可以通过正则表达式来判断。
 public static boolean isChineseChar(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        return m.find();    
    }
        String str1="你好";
        String str2="Hello";
        String str3="你好 ,@¥Hello@…… 世界.\\ ";    
        System.out.println(isChineseChar(str1)); //true
        System.out.println(isChineseChar(str2)); //false
        System.out.println(isChineseChar(str3)); //true2,获取String类型中文的数据
那么先通过正则表达式来判断是否包含中文,如果数据包含中问便将中文数据取出来。 
注:使用StringBuffer累加比String 效率更高。
public static String getChinese(String paramValue) {
        String regex = "([\u4e00-\u9fa5]+)";
        StringBuffer sb=new StringBuffer();
        Matcher matcher = Pattern.compile(regex).matcher(paramValue);
        while (matcher.find()) {
            sb.append(matcher.group(0));
        }
        return sb.toString();       
    }
       String str1="你好";
        String str2="Hello";
        String str3="你好 ,@¥Hello@…… 世界.\\ "; 
       System.out.println(getChinese(str1));  //你好
        System.out.println(getChinese(str2)); //
        System.out.println(getChinese(str3)); //你好世界3,四舍五入取整数
使用DecimalFormat来格式化十进制数字。 
注:   0 一个数字 
 # 一个数字,不包括 0 
 . 小数的分隔符的占位符
    public static Double formatDouble(Double db,String format) {
        if(null==format||"".equals(format)){
            return db;
        }
        DecimalFormat    df   = new DecimalFormat(format); //定义格式
        return  Double.parseDouble(df.format(db));
    }
        double db1=5.6849;   
        System.out.println(formatDouble(db1,"#.00")); //5.68
        System.out.println(formatDouble(db1,"#.000"));//5.685
        System.out.println(formatDouble(db1,"#")); //6.0
        System.out.println(formatDouble(db1,".00")); //5.68
        System.out.println(formatDouble(db1,"00")); //6.04,String数据中插入指定字符 
方法一:使用substring方法拼接起来
    public static String insertString1(String a,String b,int t){     
          return a.substring(0,t)+b+a.substring(t,a.length());
    } 
        String str4="abcdef";
        System.out.println(insertString1(str4,"p",2));//abpcdef
        System.out.println(insertString1(str4,"k",3));//abckdef
方法二:使用StringBuffer中insert方法
  public static String insertString2(String a,String b,int t) {
        StringBuilder  sb = new StringBuilder(a);  
        sb.insert(t, b);   
        return sb.toString();   
    }
        String str4="abcdef";
        System.out.println(insertString2(str4,"p",2));//abpcdef
        System.out.println(insertString2(str4,"k",3));//abckdef5,String数据替换指定字符 
方法一:通过在String插入字符可以得出,原该位置的字符回向后偏移,那么在拼接数据的时候去掉插入原位置的字符。
  public static String repaceString1(String a,String b,int t){     
          return a.substring(0,t)+b+a.substring(t+b.length(),a.length());
    } 
    String str4="abcdef";
    System.out.println(repaceString1(str4,"pp",2));//abppef
    System.out.println(repaceString1(str4,"kk",3));//abckkf
方法二:通过StringBuffer中的repace方法替换该字符
public static String repaceString2(String a,String b,int t){     
        StringBuilder  sb = new StringBuilder(a);  
        sb.replace(t, t+b.length(), b);   
        return sb.toString();   
    } 
    String str4="abcdef";
    System.out.println(repaceString2(str4,"pp",2));//abppef
    System.out.println(repaceString2(str4,"kk",3));//abckkf
    如果你对生活感觉到了绝望,请不要气馁。因为这样只会让你更加绝望!
所谓的希望往往都是在绝望中萌发的,所以,请不要放弃希望!


 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号