/** * 過濾掉手機號中的特殊字符:空格,‘-’等等字符。 * * @param phone : "158-8922-2222" , "158 8955 6336" * @return :phone : "15889222222" , "15889556336" */ public static String replaceSpaceStr2(String phone) { if (StringUtils.isBlank(phone)){ return ""; } StringBuffer sb = new StringBuffer(); // CharUpperLowerChange.isNumeric(phone); //判斷是否是數字 int length = phone.length(); for (int i = 0; i < length; i++) { String subStr = phone.substring(i, i + 1); if (CharUpperLowerChange.isNumeric(subStr)) { //如果這個字符是數字,則保存在sb中。 sb.append(subStr); } else { continue; } } return sb.toString(); }
/** * 判斷是否是數字 * * @param str * @return */ public static boolean isNumeric(String str) { //可以正常使用 Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); }