手机号码
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
public class PhoneUtils {
private static Pattern CHINESE_PHONE_PATTERN = Pattern.compile("^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\\d{8}$");
public static boolean isValidChinesePhone(String phone) {
if (phone == null || phone.length() != 11) {
return false;
}
Matcher matcher = CHINESE_PHONE_PATTERN.matcher(phone);
return matcher.matches();
}
public static boolean isNotValidChinesePhone(String phone) {
return !isValidChinesePhone(phone);
}
public static String setAsterisk(String phone, int beginIndex, int endIndex) {
if (StringUtils.isBlank(phone)) {
return StringUtils.EMPTY;
}
if (beginIndex < 0 || endIndex < 0 || beginIndex > phone.length() || endIndex > phone.length()) {
throw new IllegalArgumentException("illegal index " + beginIndex + "," + endIndex);
}
StringBuilder phoneWithAsterisk = new StringBuilder(phone.substring(0, beginIndex));
for (int i = beginIndex; i < endIndex; i++) {
phoneWithAsterisk.append("*");
}
phoneWithAsterisk.append(phone.substring(endIndex, phone.length()));
return phoneWithAsterisk.toString();
}
public static String setAsterisk(String phone) {
return setAsterisk(phone, 3, 7);
}
public static String setAsterisk2(String phone) {
return setAsterisk(phone, 3, 9);
}
}