Java, Check if a String is a palindrome. Case insensitive

前端 未结 5 1042
渐次进展
渐次进展 2020-12-07 03:23

I want to write a java method to return true if a string is a palindrome.

Here is what I have so far:

String palindrome = \"...\";
boolean isPalindro         


        
5条回答
  •  再見小時候
    2020-12-07 03:50

    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.

    String palindrome = "..." // from elsewhere
    String regex = "[^A-Za-z0-9]";
    boolean isPalindrome = palindrome.equals(new StringBuilder(palindrome.replaceAll(regex, "").toLowerCase()).reverse().toString());
    

提交回复
热议问题