判断字符串是否为null或者空
1 public static boolean isEmpty(final CharSequence s) { 2 return s == null || s.length() == 0; 3 }
判断去除空格后的字符串是否为null或者空
1 public static boolean isTrimEmpty(final String s) { 2 return (s == null || s.trim().length() == 0); 3 }
判断字符串是否为null或者空
1 public static boolean isSpace(final String s) { 2 if (s == null) return true; 3 for (int i = 0, len = s.length(); i < len; ++i) { 4 if (!Character.isWhitespace(s.charAt(i))) { 5 return false; 6 } 7 } 8 return true; 9 }
判断忽略大小写情况下,字符串是否equals
1 public static boolean equalsIgnoreCase(final String s1, final String s2) { 2 return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2); 3 }
判断字符串长度
1 public static int length(final CharSequence s) { 2 return s == null ? 0 : s.length(); 3 }
避免字符串null时,空指针问题
1 public static String null2Length0(final String s) { 2 return s == null ? "" : s; 3 }
字符串首字母大写
1 public static String upperFirstLetter(final String s) { 2 if (isEmpty(s) || !Character.isLowerCase(s.charAt(0))) return s; 3 return String.valueOf((char) (s.charAt(0) - 32)) + s.substring(1); 4 }
首字母小写
1 public static String lowerFirstLetter(final String s) { 2 if (isEmpty(s) || !Character.isUpperCase(s.charAt(0))) return s; 3 return String.valueOf((char) (s.charAt(0) + 32)) + s.substring(1); 4 }
判断字符串是否为纯数字
1 public static boolean isNumeric(String str){ 2 for (int i = str.length();--i>=0;){ 3 if (!Character.isDigit(str.charAt(i))){ 4 return false; 5 } 6 } 7 return true; 8 } 9 10 //使用ASCII码判断 11 public static boolean isNumeric(String str){ 12 for(int i=str.length();--i>=0;){ 13 int chr=str.charAt(i); 14 if(chr<48 || chr>57) 15 return false; 16 } 17 return true; 18 } 19 //使用正则,(推荐) 20 public static boolean isInteger(String str) { 21 Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); 22 return pattern.matcher(str).matches(); 23 } 24 //使用正则 25 public final static boolean isNumeric(String s) { 26 if (s != null && !"".equals(s.trim())) 27 return s.matches("^[0-9]*$"); 28 else 29 return false; 30 }
判断字符串是否为纯字母
1 public static boolean isNumeric(String str){ 2 for (int i = str.length();--i>=0;){ 3 if (!Character.isDigit(str.charAt(i))){ 4 return false; 5 } 6 } 7 return true; 8 } 9 10 public boolean isNumeric(String str){ 11 Pattern pattern = Pattern.compile("[0-9]*"); 12 Matcher isNum = pattern.matcher(str); 13 if( !isNum.matches() ){ 14 return false; 15 } 16 return true; 17 }
判断字符串是不是手机号
1 public boolean isMobilePhone(String mobiles) { 2 Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$"); 3 Matcher m = p.matcher(mobiles); 4 return m.matches(); 5 }
剩余部分https://blog.csdn.net/rockykou/article/details/79951907