public class StringReverseDemo {
public static void main(String[] a) {
String s = "a man,a plan,a canal, Panama";//给定的字符串
System.out.println(s + " is Palindromes? "+ (isPalindromes(s) == true ? "true" : "false"));
}
//判断是否是回文
public static boolean isPalindromes(String s) {
boolean bool = false;
String s1 = s.toLowerCase().replaceAll("[\\W\\s]", "");
char[] c = s1.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = c.length - 1; i >= 0; i--) {
sb.append(c[i]);
}
String s2 = sb.toString();
if (s1.equals(s2)) {
bool = true;
}
return bool;
}
}
public class IsHuiwen{
public static void main(String []args){
String str=args[0];
StringBuffer strbuf=new StringBuffer(str);
String str2=new String(strbuf.reverse());
if(str.equals(str2)){
System.out.println(str+"是回文");
}
else{
System.out.println(str+"不是回文");
}
}
}
字符串是否为空吗?还是空格啊?