简体   繁体   中英

Java: Remove non alphabet character from a String without regex

Is there a way to remove all non alphabet character from a String without regex? I'm trying to check if the String is a palindrome

This is what i tried so far.

    public static boolean isPalindrome( String text )
    {
        int textLength = text.length() - 1;
        String reformattedText = text.trim().toLowerCase();
        for( int i = 0; i <= textLength; i++ )
        {
            if( reformattedText.charAt( i ) != reformattedText.charAt( textLength - i ) )
            {
                return false;
            }
        }

        return true;
    }

But if the input is:

System.out.println( isPalindrome( "Are we not pure? No sir! Panama’s moody"
            + "Noriega brags. It is garbage! Irony dooms a man; a prisoner up to new era." ) );

It should be true.

I'm really having a hard time thinking of how to remove or ignore those non alphabet characters on the String.

OOPS. Java, not Python.

You can still use list-like access in Java, just a bit more work.

char[] letters = text.toCharArray(); 
int nletters = 0;
for (int i=0; i<letters.length; ++i) {
    if (Character.isLetter(letters[i])
        letters[nletters++] = Character.toUpperCase(letters[i]);
}
// print out letters in array:
System.out.print("letters only: ");
for (int i=0; i<nletters; ++i) {
    System.out.print(letters[i]);
}
System.out.println();

Now use the first nletters positions only in the letters array, since those positions will hold the lowercased letters from the input. An example that just displays the remaining characters is included above.

Now write a loop to compare letters[0] with letters[nletters-1] , letters[1] with letters[nletters-2] , and so on. If all pairs are equal, you have a palindrome.

I would do something like this:

public static String justAlphaChars(String text) {

    StringBuilder builder = new StringBuilder();

    for (char ch : text.toCharArray()) 
        if (Character.isAlphabetic(ch)) 
            builder.append(ch);

    return builder.toString();
}

Just tested method above in your example bellow and worked. Returned true.

System.out.println( isPalindrome( justAlphaChars ( "Are we not pure? No sir! Panama’s moody"
        + "Noriega brags. It is garbage! Irony dooms a man; a prisoner up to new era." ) ) );
String removeNonAlpha(final String word) {
    final StringBuilder result = new StringBuilder();

    for (final char ch : word.toCharArray()) {
        final int ascii = ch;
        if (((ascii >= 65) && (ascii <= 90)) || ((ascii >= 97) && (ascii <= 122))) {
            result.append(ch);
        }
    }
    return result.toString();
}

Explanation: The method will retrieve a string containing only AZ and az characters. I am simply verifying the ascii code for the given char. Please refer to the ASCII code table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM