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

use*_*360 7 java

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

while((s.charAt(j)== null)
Run Code Online (Sandbox Code Playgroud)

检查角色是否正确的正确方法是null什么?

Rei*_*eus 9

在进行任何字符检查之前检查String s是否没有null.返回的字符String#charAt是原始char类型,永远不会是null:

if (s != null) {
  ...
Run Code Online (Sandbox Code Playgroud)

如果您尝试一次处理String一个字符,则可以使用:

for (char c: s.toCharArray()) {
   // do stuff with char c  
}
Run Code Online (Sandbox Code Playgroud)

(不像C,NULL终止检查是不是在Java中完成的.)


Bug*_*pen 7

这里char实际描述正确的检查方式.

它指出:

将其更改为:if(position[i][j] == 0) 每个char都可以与a进行比较int.'\u0000'对于char数组元素,默认值为0 .而这正是你所说的空单元的意思.


Ras*_*ngh 7

char原语的默认值为0,作为其ascii值.你可以检查char是否为null.例如:

char ch[] = new char[20]; //here the whole array will be initialized with '\u0000' i.e. 0
    if((int)ch[0]==0){
        System.out.println("char is null");
    }
Run Code Online (Sandbox Code Playgroud)