How to check if a character is an apostrophe?

26,991

You could simply use if(c=='\'') without cast. Or you can use ascii value of apostrophe which is 39. if (c==39) will do.

it never actually gets into the 'if ((char) c == '\'') part

The only reason for this could you never pass apostrophe to isWordCharacter(). You can verify it by manually sending 39 or '\'' to that function.

Share:
26,991
flymonkey
Author by

flymonkey

Updated on July 19, 2022

Comments

  • flymonkey
    flymonkey almost 2 years

    I need to check if a character is an apostrophe. This is my code so far:

    public boolean isWordCharacter(int c) {
    if ((char) c == '\'')
        return true;
    else return Character.isLetter(c);
    }
    

    However, it never actually gets into the if ((char) c == '\'') part. Is there something wrong with the way I check it? Thanks!