Java 如何检查字符是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50663168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:44:24  来源:igfitidea点击:

How to check if a char is null

javaandroid

提问by Cheeseburger

I want to check if the char is null or not? but why this code is not working? letterChar == nullalso is not working. I googled for many problems but didn't see any solutions, mostly the solutions are about String.

我想检查字符是否为空?但为什么这段代码不起作用?letterChar == null也不起作用。我用谷歌搜索了很多问题,但没有看到任何解决方案,大多数解决方案都是关于String.

String letter = enterletter.getText().toString();
char letterChar = letter.charAt(0);


if(letterChar == ' ' || letterChar == NULL) // this is where the code won't works
{
    Toast.makeText(getApplicationContext(), "Please enter a letter!", Toast.LENGTH_LONG).show();
}

采纳答案by shmuels

A charcannot be nullas it is a primitive so you cannot check if it equals null, you will have to find a workaround.

Achar不能是,null因为它是一个原语,所以你不能检查它是否等于null,你必须找到一个解决方法。

Also did you want to check if letterChar == ' 'a space or an empty string? Since you have a space there.

您还想检查letterChar == ' '空格还是空字符串?因为你那里有空间。

The first two answers heremay be helpful for how you can eithercheck if String letteris null first.

前两个答案在这里可能是你怎么能有帮助或者检查是否String letter为空第一。

orcast char letterCharinto an int and check if it equals 0since the default value of a char is \u0000- (the nul character on the ascii table, not the null reference which is what you are checking for when you say letterChar == null)- which when cast will be 0.

char letterChar成int和检查,如果它等于0从一个字符的默认值是\u0000- (空字符的ASCII表,而不是空引用这是你正在检查时,你说的letterChar == null) -这施放时会0

回答by Daria Yershova

From your code it seems like you work with Unicode and the method you search for is into Characterclass (java.lang)

从您的代码来看,您似乎使用 Unicode,并且您搜索的方法是Character类(java.lang)

Character.isLetter(ch);

It also contains lots of useful static method that can suite you. If so, the code will be

它还包含许多可以适合您的有用的静态方法。如果是这样,代码将是

String letter = enterletter.getText().toString();
char letterChar = letter.charAt(0);

if(!Character.isLetter(letterChar))
{
    Toast.makeText(getApplicationContext(), "Please enter a letter!", Toast.LENGTH_LONG).show();
}

But if I would need to answer your question (without taking a look to your aim), then the code you need depends on what do you mean by "is char null".

但是,如果我需要回答您的问题(不考虑您的目标),那么您需要的代码取决于您所说的“is char null”是什么意思。

In Java char cannot be == null. If you want to check if it is not an empty space, you need to do

在 Java 中,char 不能为 == null。如果你想检查它是否不是一个空的空间,你需要做

 if (ch == ' ')

If you want to check if there are no line breakers (space, HT, VT, CR, NL and other), you should add the following to the proposed above:

如果要检查是否有线路断路器(空格、HT、VT、CR、NL 等),则应在上述建议中添加以下内容:

if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\x0b')