如何比较一个字符以检查它是否为空?
问题描述:
我尝试了下面的内容,但Eclipse为此抛出了一个错误。
I tried the below, but Eclipse throws an error for this.
while((s.charAt(j)== null)
检查字符是否为 null ?
What's the correct way of checking whether a character is null
?
答
检查字符串
s 不是 null
。 String#charAt返回的字符
是原始的 char
类型,永远不会是 null
:
Check that the String
s
is not null
before doing any character checks. The characters returned by String#charAt
are primitive char
types and will never be null
:
if (s != null) {
...
如果您尝试一次处理一个 String
中的字符,您可以使用:
If you're trying to process characters from String
one at a time, you can use:
for (char c: s.toCharArray()) {
// do stuff with char c
}
(与 C
不同, NULL
终止符检查不是用Java完成的。)
(Unlike C
, NULL
terminator checking is not done in Java.)