Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a character is an apostrophe?

Tags:

java

char

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!

like image 768
flymonkey Avatar asked Apr 10 '12 00:04

flymonkey


People also ask

How do you check if a string has an apostrophe?

We have a global variable myString which holds a string as its value. In the event handler function, we are using the includes() method and ternary operator ( ? ) to verify whether myString contains apostrophe or not. Depending upon the result of the check, we will assign “Yes” or “No” to the result variable.

What type of character is an apostrophe?

An apostrophe is commonly used to indicate omitted characters, normally letters: It is used in contractions, such as can't from cannot, it's from it is or it has, and I'll from I will or I shall.

How do you encode an apostrophe?

Encoding. The letter apostrophe is encoded at U+02BC ʼ MODIFIER LETTER APOSTROPHE. In Unicode code charts it looks identical to the U+2019 ' RIGHT SINGLE QUOTATION MARK, but this is not true for all fonts.


1 Answers

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.

like image 129
P.P Avatar answered Oct 18 '22 01:10

P.P