回文串判断
1、直接上代码
public static boolean isPalindrome(String s) {
//1、判断字符串是否是null或者是空字符,如果是就返回true
if (s == null && "".equals(s.trim())) {
return true;
}
//2、移除非字母数字字符,并将大写字母转化为小写字母
StringBuilder filterStr = new StringBuilder("");
for (int i = 0; i < s.length(); i++) {
char currChat = s.charAt(i);
if (('A' <= currChat && currChat <= 'Z') || ('a' <= currChat && currChat <= 'z') || ('0' <= currChat && currChat <= '9')) {
filterStr.append(String.valueOf(currChat).toLowerCase());
}
}
//验证字符串是否是回文串
char [] checkStr = filterStr.toString().toCharArray();
int minLength = checkStr.length / 2 ;
for (int j = 0; j < minLength; j++) {
if (checkStr[j] != checkStr[checkStr.length - 1 - j]) {
return false;
}
}
return true;
}
缘于生活,而归于工作。本人所书,而意于分享。
如有转载,请注明出处!
--活出自己范儿

浙公网安备 33010602011771号