如何比较一个字符以检查它是否为空? [英] How do I compare a character to check if it is null?

查看:128
本文介绍了如何比较一个字符以检查它是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了下面的内容,但Eclipse为此抛出了一个错误。

I tried the below, but Eclipse throws an error for this.

while((s.charAt(j)== null)

检查字符是否为 null <的正确方法是什么? / code>?

What's the correct way of checking whether a character is null?

推荐答案

检查字符串 <在进行任何字符检查之前,code> 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.)

这篇关于如何比较一个字符以检查它是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆