且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何比较一个字符以检查它是否为空?

更新时间:2023-11-17 17:28:40

检查字符串 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.)