2
我需要检查回文在一个单独的类,但忽略非字母字符。因此,例如,雷达将仍有资格,如果它被写R,A,d,A,R检查回文时,如何忽略空格,标点符号和与字母不同的所有字符?
我相信我可以使用正则表达式,但我不知道怎么办。
这里是我到目前为止,
public static boolean isNonAlpha(char c) {
return (c == '-' || c == '.' || c == ' ' || c == ')' || c == '(') || c == '<' || c == '>' || c == ',';
}
public static String checkInput(String test){
int startChar = 0;
int endChar = test.length() - 1;
while (startChar < endChar) {
if (test.charAt(startChar) != test.charAt(endChar)) {
System.out.println("Your word is not a palindrome.");
System.exit(0);
} else {
if (test.charAt(startChar) == test.charAt(endChar))
startChar++;
endChar--;
}
}
System.out.println("Your word is indeed a palindrome.");
return test;
}
我卡在如何将我的isNonAlpha方法,或如何使用正则表达式
标准的正则表达式无法检测回文(高达无限的价值)的东西是以前所接受。 http://stackoverflow.com/questions/233243/how-to-check-that-a-string-is-a-palindrome-using-regular-expressions但是有一些页面 –
在一些实现可能可以做在Java类的正则表达式实现如图有些语言虽然,但他们在技术上都是不正规的 –
[删除所有非字母字符(http://stackoverflow.com/questions/11149759/remove-all-non-alphabetic-characters-从-A-串阵列式-java的),并使用[定期回文校验码(http://stackoverflow.com/a/4138856/3832970)。请参阅[本演示](http://ideone.com/ALr1Sl)。它按预期工作吗? –