(Java) to judge a palindrome string, ignoring characters that are not alphanumeric or not

Source: Internet
Author: User
Tags alphanumeric characters stringbuffer

Question: Detect whether the string is a palindrome string, excluding characters that are not letters and numbers


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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.