Question: Detect whether the string is a palindrome string, excluding characters that are not letters and numbers
Solve:
1. Filter strings by removing non-alphanumeric characters
Creates an empty string buffer, adds each alphanumeric character in the string to a character buffer, and returns a string from the buffer, using the Isletterordigit (ch) method in the character class to detect whether the character Ch is alphanumeric.
2, inverted filtered string to get a new string, using the Equals () method to compare the inverted string and filtered string content is equal.
List of programs: Checkhuiwenstringignorenotletteranddidgit.java
to ignore characters that are not alphanumeric or not, judge a palindrome string public
class Checkhuiwenstringignorenotletteranddidgit {public
static void main ( String[] args) {
String s= "A?B2C\\D1E1%DC/2BA";
System.out.println ("is" + "[" +s+ "]" + "palindrome string? + "--" +ishuiwen (s));
String ss= "12?abc" (BA12);
System.out.println ("is" + "[" +ss+ "]" + "palindrome string? + "--" +ishuiwen (ss));
}
public static Boolean Ishuiwen (string s) {
string s1=filter (s);
String S2=reverse (S1);
Return s2.equals (S1);
}
public static string filter (string s) {//remove non-alphanumeric character filter strings
StringBuffer strbuf=new stringbuffer ();
for (int i=0;i<s.length (); i++) {
if (Character.isletterordigit (S.charat (i))
strbuf.append (S.charat (i) ));
}
return strbuf.tostring ();
}
public static string reverse (string s) {//filtered string inversion, returns the new string
StringBuffer strbuf=new stringbuffer (s);
Strbuf.reverse ();
return strbuf.tostring ();
}