Java,检查字符串是否为回文。不区分大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15401630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java, Check if a String is a palindrome. Case insensitive
提问by user2121604
I want to write a java method to return true if a string is a palindrome.
如果字符串是回文,我想编写一个 java 方法来返回 true。
Here is what I have so far:
这是我到目前为止所拥有的:
String palindrome = "...";
boolean isPalindrome = palindrome.equals(
new StringBuilder(palindrome).reverse().toString());
My problem with this is that it does not consider a word like: Race car
to be a palindrome.
我的问题是它不考虑这样的词:Race car
成为回文。
Doc, note, I dissent. A fast never prevents a fatness. I diet on cod.
What is the best way to test if this is a palindrome, with case insensitivity and ignoring punctuation.
测试这是否是回文的最佳方法是什么,不区分大小写并忽略标点符号。
回答by Fr_nkenstien
Use this regex to remove all punctuation and spaces and convert it to lower case
使用此正则表达式删除所有标点符号和空格并将其转换为小写
String palindrome = "..." // from elsewhere
boolean isPalindrome = palindrome.replaceAll("[^A-Za-z]", "").toLowerCase().equals(new StringBuilder(palindrome.replaceAll("[^A-Za-z]", "").toLowerCase()).reverse().toString());
回答by Sudhanshu Umalkar
Try this ..
试试这个 ..
public static void main(String[] args) {
boolean notPalindrome = false;
String string = "Doc, note, I dissent. A fast never prevents a fatness. I diet on cod";
string = string.replaceAll("[^a-zA-Z]+","").toLowerCase();
char[] array = string.toCharArray();
for(int i=0, j=array.length-1; i<j; i++, j--) {
if(array[i] != array[j]) {
notPalindrome = true;
break;
}
}
System.out.println(string + " is palindrome? " + !notPalindrome);
}
回答by SudoRahul
Use the below regex, to keep even numeric
characters in the Palindrome, if needed. Else, you can just remove the 0-9
from the regex.
numeric
如果需要,使用下面的正则表达式在回文中保留偶数字符。否则,您可以0-9
从正则表达式中删除。
String palindrome = "..." // from elsewhere
String regex = "[^A-Za-z0-9]";
boolean isPalindrome = palindrome.equals(new StringBuilder(palindrome.replaceAll(regex, "").toLowerCase()).reverse().toString());
回答by Bhushan
Here is a non regex
solution.
这是一个非regex
解决方案。
public class so4
{
public static void main(String args[])
{
String str = "Doc, note, I dissent. A fast never prevents a fatness. I diet on cod";
char c[] =str.toCharArray();
String newStr="";
for(int i=0;i<c.length;i++)
{
if( (c[i]>=65 && c[i]<=90) || (c[i]>=97 && c[i]<=122)) //check ASCII values (A-Z 65-90) and (a-z 97-122)
{
newStr = newStr + c[i];
}
}
boolean isPalindrome = newStr.toLowerCase().equals(new StringBuilder(newStr.toLowerCase()).reverse().toString());
System.out.println(isPalindrome);
}
}
回答by jahroy
convert to lower case
use a regex to remove everything but letters
reverse the string using a StringBuilder
compare the strings for equality
转换为小写
使用正则表达式删除除字母以外的所有内容
使用StringBuilder反转字符串
比较字符串是否相等
Code:
代码:
/**
* Returns true if s is a palindrome, ignoring whitespace
* punctuation, and capitalization. Returns false otherwise.
*/
public boolean isPalindrome(String s) {
String forward = s.toLowerCase().replaceAll("[^a-z]", "");
String reverse = new StringBuilder(forward).reverse().toString();
return forward.equals(reverse);
}
For more info, see the documentation for Stringand StringBuilder:
有关更多信息,请参阅String和StringBuilder的文档:
You can also find it by googling "Java 7 String" and clicking the first result.
您也可以通过谷歌搜索“ Java 7 String”并单击第一个结果来找到它。