public static String reverseStr(String str) { char[] chars = str.toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = chars.length - 1; i >= 0; i--) { sb.append(chars[i]); } return sb.toString(); } public static String test() { String str = "qwertyuiop1234567890987654321zxcvbnm,"; //System.out.println(str); String max = ""; int count = 0; //起始位置开始扫描 for (int i = 0; i < str.length(); i++) { //结束位置开始扫描 for (int j = str.length(); (j >= i)&(j-i>=max.length()); j--) { count++; String substr = str.substring(i, j); //截取一个字符串 int length = substr.length(); //判断是否对称 int middle = 0; if (substr.length() % 2 == 0) { middle = substr.length() / 2; } else { middle = substr.length() / 2 + 1; } //如果对称赋值给max if (substr.substring(0, substr.length() / 2).equals( reverseStr(substr.substring(middle)))) { if(max.length()<substr.length()){ max = substr; } } } } System.out.println(count); return max; }