How to check if a char is equal to an empty space?

delete picture delete · Dec 22, 2010 · Viewed 507.5k times · Source

Here's what I've got:

private static int countNumChars(String s) {
    for(char c : s.toCharArray()){
        if (Equals(c," "))
    }
}

But that code says it cannot find Symbol for that method. I remember Java having a comparer like this... Any suggestions?

Answer

Nikita Rybak picture Nikita Rybak · Dec 22, 2010
if (c == ' ')

char is a primitive data type, so it can be compared with ==.

Also, by using double quotes you create String constant (" "), while with single quotes it's a char constant (' ').