可以用以下几种方法来判断一个字符串是否为回文:
public static boolean isPalindrome(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString().equals(str); } public static boolean isPalindrome(String str) { int start = 0; int end = str.length() - 1; while (start < end) { if (str.charAt(start) != str.charAt(end)) { return false; } start++; end--; } return true; } public static boolean isPalindrome(String str) { if (str.length() <= 1) { return true; } if (str.charAt(0) != str.charAt(str.length() - 1)) { return false; } return isPalindrome(str.substring(1, str.length() - 1)); } 以上是三种常见的判断字符串是否为回文的方法。可以根据实际情况选择适合的方法来使用。