Android開發中常用字符處理工具類

f663x 9年前發布 | 1K 次閱讀 Java Android

發現開發了幾個項目都要用到這些字符處理,資料雖少,但還是簡單做了記錄,以便以后用得上。

public class VerifyTool {

/*
 * 郵箱驗證
 */
public static boolean isEmail(String email) {
    return Pattern.compile("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$").matcher(email).matches();
}

/**
 * 驗證密碼格式, 只支持英文和數字.
 *
 */
public static boolean verifyPasswordFormat(String pwd) {
    return Pattern.compile("[a-zA-Z0-9]*").matcher(pwd).matches();
}

/**
 *
 * @description:驗證手機號是否正確(這是根據我們項目需要進行的,實際的不一定都這樣)
 */
public static boolean isMobileNO(String telephone) {
    if (TextUtils.isEmpty(telephone))
        return false;

// Pattern pattern = Pattern.compile("^0?(13[0-9]|15[012356789]|17[012356789]|18[012356789]|14[57])[0-9]{8}$"); Pattern pattern = Pattern.compile("^1[3,4,5,7,8][0-9]\d{8}$"); Matcher matcher = pattern.matcher(telephone); return matcher.matches(); }

/**
 * 驗證電話號碼及是否正確;
 *
 */
public static boolean isPhoneNO(String mobiles) {
    Pattern p = Pattern.compile("^\\d{10,12}$");
    Matcher m = p.matcher(mobiles);
    return m.matches();
}

/**
 * 驗證6位短信驗證碼是否正確;
 *
 */
public static boolean isSmsCode(String msgCode) {
    Pattern p = Pattern.compile("^\\d{6}$");
    Matcher m = p.matcher(msgCode);
    return m.matches();
}

/**
 * 驗證IP和端口的格式是否有誤
 *
 * @param ip
 *            訪問服務器 IP 地址
 * @param port
 *            訪問服務器端口
 * @return 校驗ip和端口 return -1 ip 或者 port 為空. -2 ip格式錯誤. -3 port為數字 0 通過
 * */
public static final int verificateIPAndPort(String ip, String port) {

    if (null == ip || "".equals(ip) || null == port || "".equals(port)) {
        return -1;
    }

    try {
        Integer.parseInt(port);
    }
    catch (NumberFormatException e) {
        return -3;
    }

    Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
    Matcher matcher = pattern.matcher(ip.trim());
    if (matcher.matches()) {
        return 0;
    }
    else {
        return -2;
    }

}



/**
 *
 * 方法概述:
 *
 * @description 方法詳細描述:驗證輸入的金額為整數且只能有兩個位小數
 */
public static boolean monneyFormate(String s) {
    if (TextUtils.isEmpty(s))
        return false;

    Pattern pattern = Pattern.compile("^(-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){1,2})?$");
    return pattern.matcher(s).matches();
}

/**
 * (判斷字符串的長度)
 */
public static boolean isCheckLength(String str, int start, int end) {
    Pattern pattern = Pattern.compile("^.{" + start + "," + end + "}$");
    return pattern.matcher(str).matches();
}

/**
 * @author:tsp
 * @Title: isLength
 * @Description: TODO(判斷身份證的長度)
 * @param @param str
 * @param @return
 * @return boolean
 * @throws
 */
public static boolean isCheckCardLenth(String str, int start, int end) {
    Pattern pattern = Pattern.compile("^[A-Za-z0-9].{" + start + "," + end + "}$");
    return pattern.matcher(str).matches();
}


/**
 *
 * @description:判斷是否是身份證格式
 */
public static boolean checkCardNo(String cardNo) {
    Pattern pattern = Pattern.compile("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)");
    Matcher matcher = pattern.matcher(cardNo);
    return matcher.matches();
}

/**
 * 判斷中文
 */
public static boolean isChineseChar(String inputString) {
    Pattern pattern = Pattern.compile("^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$");

// Pattern pattern = Pattern.compile("^[\u4E00-\u9FA5]"); return pattern.matcher(inputString).matches(); }

/**
 * 匹配非負浮點數
 */
public static boolean isRealNumber(String inputString) {
    Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]{1,5})?$");
    return pattern.matcher(inputString).matches();
}

/**
 * 匹配非負浮點數
 */
public static boolean isNotNegativeFloat(String inputString) {
    Pattern pattern = Pattern.compile("^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$");
    return pattern.matcher(inputString).matches();
}

/**
 * 手機號碼截取成130****123類型字段,因為用之前方式 的話,諸如17999999990類似號碼會截取成:17********0
 */
public static  String splitePhone(String phone) {
    String[] tel = new String[phone.length()];
    StringBuffer sb = new StringBuffer();
    if (tel.length > 0) {
        for (int i = 0; i < tel.length; i++) {
            if (i > 2 && i < 7) {
                sb.append("*");
            } else {
                sb.append(phone.charAt(i));
            }
        }
    }
    return sb.toString();
}

}</pre>

 本文由用戶 f663x 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!