如何检查一个字符是否等于一个空格?

2022-01-12 00:00:00 whitespace char java

这是我得到的:

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

但是该代码说它找不到该方法的符号.我记得 Java 有一个这样的比较器...有什么建议吗?

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

推荐答案

if (c == ' ')

char 是原始数据类型,因此可以与 == 进行比较.

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

此外,通过使用双引号,您可以创建 String 常量 (" "),而使用单引号它是一个 char 常量 (' ').

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

相关文章