判断字符串是否为null或者空
public static boolean isEmpty (final CharSequence s) {
return s == null || s.length() == 0 ;
}
判断去除空格后的字符串是否为null或者空
public static boolean isTrimEmpty (final String s) {
return (s == null || s.trim().length() == 0 );
}
判断字符串是否为null或者空
public static boolean isSpace (final String s) {
if (s == null ) return true ;
for (int i = 0 , len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false ;
}
}
return true ;
}
判断忽略大小写情况下,字符串是否equals
public static boolean equalsIgnoreCase (final String s1, final String s2) {
return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2);
}
避免字符串null时,空指针问题
public static String null2Length0 (final String s) {
return s == null ? "" : s;
}
判断字符串长度
public static int length (final CharSequence s) {
return s == null ? 0 : s.length();
}
字符串首字母大写
public static String upperFirstLetter (final String s) {
if (isEmpty(s) || !Character.isLowerCase(s.charAt(0 ))) return s;
return String.valueOf((char ) (s.charAt(0 ) - 32 )) + s.substring(1 );
}
首字母小写
public static String lowerFirstLetter (final String s) {
if (isEmpty(s) || !Character.isUpperCase(s.charAt(0 ))) return s;
return String.valueOf((char ) (s.charAt(0 ) + 32 )) + s.substring(1 );
}
判断字符串是否纯数字
public static boolean isNumeric (String str){
for (int i = str.length();--i>=0 ;){
if (!Character.isDigit(str.charAt(i))){
return false ;
}
}
return true ;
}
public static boolean isNumeric (String str){
for (int i=str.length();--i>=0 ;){
int chr=str.charAt(i);
if (chr<48 || chr>57 )
return false ;
}
return true ;
}
public static boolean isInteger (String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$" );
return pattern.matcher(str).matches();
}
public final static boolean isNumeric (String s) {
if (s != null && !"" .equals(s.trim()))
return s.matches("^[0-9]*$" );
else
return false ;
}
判断字符串是否整数
public static boolean isNumeric(String str ){
for (int i = str .length();--i>=0 ;){
if (!Character.isDigit(str .charAt(i))){
return false ;
}
}
return true ;
}
public boolean isNumeric(String str ){
Pattern pattern = Pattern.compile("[0-9]*" );
Matcher isNum = pattern.matcher(str );
if ( !isNum.matches() ){
return false ;
}
return true ;
}
判断字符串是否纯字母
public boolean isLetter(String str ){
Pattern pattern = Pattern.compile("[a-zA-Z] + " );
Matcher isLetter = pattern.matcher(str );
if ( !isLetter.matches() ){
return false ;
}
}
判断字符串是否手机号码
public boolean isMobilePhone (String mobiles) {
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$" );
Matcher m = p.matcher(mobiles);
return m.matches();
}
判断提取字符串中手机号码
private static String checkNum (String num){
if (num == null || num.length() == 0 ){return "" ;}
Pattern pattern = Pattern.compile("(?<!\\d)(?:(?:1[358]\\d{9})|(?:861[358]\\d{9}))(?!\\d)" );
Matcher matcher = pattern.matcher(num);
StringBuffer bf = new StringBuffer(64 );
while (matcher.find()) {
bf.append(matcher.group ()).append("," );
}
int len = bf.length();
if (len > 0 ) {
bf.deleteCharAt(len - 1 );
}
return bf.toString();
}
判断字符串,手机号码隐藏中间四位
public static String getSafePhone(String phone){
if (phone == null || phone.length() == 0 ) return ;
String phoneNumber = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})" ,"$1****$2" );
return phoneNumber;
}
判断字符串,银行卡号隐藏中间八位
public static String getSafeCardNum(String bankCard){
if (bankCard == null || bankCard.length() == 0 ) return ;
int hideLength = 8 ;
int sIndex = bankCard.length()/2 - hideLength/2 ;
String replaceSymbol = "*" ;
StringBuilder sBuilder = new StringBuilder();
for (int i = 0 ; i<bankCard.length();i++){
char number = bankCard.charAt(i);
if (i >= sIndex-1 && i<sIndex+hideLength){
sBuilder.append(replaceSymbol);
}else {
sBuilder.append(number);
}
}
return sBuilder.toString();
}