Java 判斷是否為漢字 判斷是否為亂碼 判斷字符串是否為雙整型數字 整數 數字


/**
  * 判斷是否為漢字
  * 
  * @param str
  * @return
  */
 public static boolean isGBK(String str) {
  char[] chars = str.toCharArray();
  boolean isGBK = false;
  for (int i = 0; i < chars.length; i++) {
   byte[] bytes = ("" + chars[i]).getBytes();
   if (bytes.length == 2) {
    int[] ints = new int[2];
    ints[0] = bytes[0] & 0xff;
    ints[1] = bytes[1] & 0xff;
    if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40
      && ints[1] <= 0xFE) {
     isGBK = true;
     break;
    }
   }
  }
  return isGBK;
 }

/**
  * 判斷是否為亂碼
  * 
  * @param str
  * @return
  */
 public static boolean isMessyCode(String str) {
  for (int i = 0; i < str.length(); i++) {
   char c = str.charAt(i);
   // 當從Unicode編碼向某個字符集轉換時,如果在該字符集中沒有對應的編碼,則得到0x3f(即問號字符?)
   //從其他字符集向Unicode編碼轉換時,如果這個二進制數在該字符集中沒有標識任何的字符,則得到的結果是0xfffd
   //System.out.println("--- " + (int) c);
   if ((int) c == 0xfffd) {
    // 存在亂碼
    //System.out.println("存在亂碼 " + (int) c);
    return true;
   }
  }
  return false; 
 }


 /**
  * 判斷字符串是否為雙整型數字
  * 
  * @param str
  * @return
  */
 public static boolean isDouble(String str) {
  if (StringUtil.isNullOrEmpty(str)) {
   return false;
  }
  Pattern p = Pattern.compile("-*\\d*.\\d*");
  // Pattern p = Pattern.compile("-*"+"\\d*"+"."+"\\d*");
  return p.matcher(str).matches();
 }

 /**
  * 判斷字符串是否為整字
  * 
  * @param str
  * @return
  */
 public static boolean isNumber(String str) {
  if (StringUtil.isNullOrEmpty(str)) {
   return false;
  }
  Pattern p = Pattern.compile("-*\\d*");
  return p.matcher(str).matches();
 }
 
 /**
  * 判斷是否為數字
  * 
  * @param str
  * @return
  */
 public static boolean isNumeric(String str)   
 {   
         Pattern pattern = Pattern.compile("[0-9]*");   
         Matcher isNum = pattern.matcher(str);   
        if( !isNum.matches() ) {   
           return false;   
         }   
        return true;   
 }

 

需要導入 java.util.regex.Pattern 和 java.util.regex.Matcher

 

from:http://hi.baidu.com/zdz8207/item/13abe809f904c718eafe38d5


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM