how to check if a character is equal to double quote in java

Mehdi Ijadnazar picture Mehdi Ijadnazar · Feb 6, 2011 · Viewed 38.4k times · Source

I want to check the value of a char to see if it is double quote or not in Java. How can I do it?

Answer

JB Nizet picture JB Nizet · Feb 6, 2011
if (myChar == '"') { // single quote, then double quote, then single quote
    System.out.println("It's a double quote");
}

If you want to compare a String with another one, and test if the other string only contains the double quote char, then you must escape the double quote with a \ :

if ("\"".equals(myString)) {
    System.out.println("myString only contains a double quote char");
}