1.字符串判断是否为空
/**
* 判断是否为空
*
* @param text
* @return
*/
public static boolean isNullOrEmpty(String text) {
if (text == null || "".equals(text.trim()) || text.trim().length() == 0
|| "null".equals(text.trim())) {
return true;
} else {
return false;
}
}
2.判断字符串数组texts中是否有一个字符串为空
/**
* 判断字符串数组texts中是否有一个字符串为空
*
* @param texts
* @return 如果字符串数组texts中有一个为空或texts为空,返回true;otherwise return false;
*/
public static boolean isEmpty(String... texts) {
if (texts == null || texts.length == 0) {
return true;
}
for (String text : texts) {
if (text == null || "".equals(text.trim()) || text.trim().length() == 0
|| "null".equals(text.trim())) {
return true;
}
}
return false;
}
3.获得MD5加密字符串
/**
* 获得MD5加密字符串
*
* @param str
* 字符串
* @return
*/
public static String getMD5Str(String str) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
4.得到字符串长度
/**
* 得到字符串长度
*
* @param text
* @return
*/
public static int getCharCount(String text) {
String Reg = "^[\u4e00-\u9fa5]{1}$";
int result = 0;
for (int i = 0; i < text.length(); i++) {
String b = Character.toString(text.charAt(i));
if (b.matches(Reg))
result += 2;
else
result++;
}
return result;
}
5.获取截取后的字符串
/**
* 获取截取后的字符串
*
* @param text
* 原字符串
* @param length
* 截取长度
* @param isOmit
* 是否加上省略号
* @return
*/
public static String getSubString(String text, int length, boolean isOmit) {
if (isNullOrEmpty(text)) {
return "";
}
if (getCharCount(text) <= length + 1) {
return text;
}
StringBuffer sb = new StringBuffer();
String Reg = "^[\u4e00-\u9fa5]{1}$";
int result = 0;
for (int i = 0; i < text.length(); i++) {
String b = Character.toString(text.charAt(i));
if (b.matches(Reg)) {
result += 2;
} else {
result++;
}
if (result <= length + 1) {
sb.append(b);
} else {
if (isOmit) {
sb.append("...");
}
break;
}
}
return sb.toString();
}
6.电话号码验证
/**
* 电话号码验证
*
* @param phoneNumber
* 手机号码
* @return
*/
public static boolean validatePhoneNumber(String phoneNumber) {
Pattern pattern = Pattern
.compile("^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");
Matcher m = pattern.matcher(phoneNumber);
return m.matches();
}