工具类 – 个人常用工具类String
public class StringUtile{
public static final String REGEX_UUID = "[0-9a-zA-Z]{32}";
private static final String cityCode[] = {"11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34",
"35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64",
"65", "71", "81", "82", "91"};
private static int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
private static final String phoneRegular = "^1[0-9]{10}$";
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
public static String shortenByChars(String string, int size) {
if (size < 0) {
throw new IllegalArgumentException("size can not be negative");
}
if (string == null || string.length() <= size) {
return string;
}
if (size < 3) {
return string.substring(0, size);
}
return string.substring(0, size - 3) + "...";
}
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
public static String subZeroAndDot(String s) {
if (s.indexOf(".") > -1) {
s = s.replaceAll("0+?$", "");
s = s.replaceAll("[.]$", "");
}
return s;
}
public static String getHiddenPhone(String phone) {
String hiddenPhone = phone;
if (isEmpty(phone)) {
hiddenPhone = "";
}
if (phone.length() == 11) {
hiddenPhone = phone.substring(0, 3) + "****" + phone.substring(7);
}
return hiddenPhone;
}
public static String getUuid32() {
return UUID.randomUUID().toString().replace("-", "");
}
public static boolean checkIsPhone(String phone) {
if (isEmpty(phone)) {
return false;
}
return phone.matches(phoneRegular);
}
public static boolean isNotNull(String s) {
if ((null != s) && (s.trim().length() != 0))
return true;
return false;
}
public static boolean isNull(String s) {
if ((null == s) || (s.trim().length() == 0))
return true;
return false;
}
private static boolean checkProvinceid(String provinceid) {
for (String id : cityCode) {
if (id.equals(provinceid)) {
return true;
}
}
return false;
}
public static String getHiddenEmail(String email) {
String hiddenEmail = email;
if (isEmpty(email)) {
hiddenEmail = "";
}
if (email.contains("@")) {
String[] emails = email.split("@");
String name = emails[0];
if (name.length() > 1) {
name = name.substring(0, 2) + "***" + name.substring(name.length() - 1);
} else {
name = name + name + "***" + name;
}
hiddenEmail = name + "@" + emails[1];
}
return hiddenEmail;
}
public static boolean isUuid(String str) {
if (isBlank(str)) {
return false;
}
if (length(str) != 32) {
return false;
}
return Pattern.matches(REGEX_UUID, str);
}
public static String addZeroLeft(String str, int strLength) {
int strLen = str.length();
if (strLen < strLength) {
while (strLen < strLength) {
StringBuffer sb = new StringBuffer();
sb.append("0").append(str);
str = sb.toString();
strLen = str.length();
}
}
return str;
}
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
private static boolean isDigital(String str) {
return str.matches("^[0-9]*$");
}
public static boolean validate18Idcard(String idcard) {
if (idcard == null) {
return false;
}
if (idcard.length() != 18) {
return false;
}
String idcard17 = idcard.substring(0, 17);
if (!isDigital(idcard17)) {
return false;
}
String provinceid = idcard.substring(0, 2);
if (!checkProvinceid(provinceid)) {
return false;
}
String birthday = idcard.substring(6, 14);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try {
Date birthDate = sdf.parse(birthday);
String tmpDate = sdf.format(birthDate);
if (!tmpDate.equals(birthday)) {
return false;
}
} catch (ParseException e1) {
return false;
}
String idcard18Code = idcard.substring(17, 18);
char c[] = idcard17.toCharArray();
int bit[] = converCharToInt(c);
int sum17 = 0;
sum17 = getPowerSum(bit);
String checkCode = getCheckCodeBySum(sum17);
if (null == checkCode) {
return false;
}
if (!idcard18Code.equalsIgnoreCase(checkCode)) {
return false;
}
return true;
}
private static int[] converCharToInt(char[] c) throws NumberFormatException {
int[] a = new int[c.length];
int k = 0;
for (char temp : c) {
a[k++] = Integer.parseInt(String.valueOf(temp));
}
return a;
}
private static int getPowerSum(int[] bit) {
int sum = 0;
if (power.length != bit.length) {
return sum;
}
for (int i = 0; i < bit.length; i++) {
for (int j = 0; j < power.length; j++) {
if (i == j) {
sum = sum + bit[i] * power[j];
}
}
}
return sum;
}
private static String getCheckCodeBySum(int sum17) {
String checkCode = null;
switch (sum17 % 11) {
case 10:
checkCode = "2";
break;
case 9:
checkCode = "3";
break;
case 8:
checkCode = "4";
break;
case 7:
checkCode = "5";
break;
case 6:
checkCode = "6";
break;
case 5:
checkCode = "7";
break;
case 4:
checkCode = "8";
break;
case 3:
checkCode = "9";
break;
case 2:
checkCode = "x";
break;
case 1:
checkCode = "0";
break;
case 0:
checkCode = "1";
break;
}
return checkCode;
}
}