java 如何检查字符串是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12853577/
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-10-31 10:29:23  来源:igfitidea点击:

How to check if String is empty?

javastring

提问by Subhrajyoti Majumder

I am using following code to check if the String is empty

我正在使用以下代码检查字符串是否为空

if( !Index2.toString().isEmpty()){
  ....
}

But I run into following error

但我遇到了以下错误

java.lang.NullPointerException

回答by Luiggi Mendoza

Your Index2variable has nullvalue. That's why you get the error. Specifically, the error is here:

你的Index2变量null有价值。这就是你得到错误的原因。具体来说,错误在这里:

Index2.toString()

Assuming your Index2variable is different from null, your code will work. In order to make your code to work, you can validate that your Index2is different from nullor add a try/catchblock that handles NullPointerException.

假设您的Index2变量不同于null,您的代码将起作用。为了使你的代码的工作,你可以验证你Index2是从不同null或添加try/catch块来处理NullPointerException

First way:

第一种方式:

if( Index2 != null && !Index2.toString().isEmpty()){
    //...
}

Second way:

第二种方式:

try {
    if( !Index2.toString().isEmpty()){
      //...
    }
} catch (NullPointerException npe) {
    //handle the Exception...
}

IMHO I would use the first way.

恕我直言,我会使用第一种方式。

回答by csk

Check for String not to be null as well check for the empty space in a String because even if we have a space in a String as myString=" "; myString.isEmpty() will return false . so please considering the case above i will suggest as :

检查 String 是否为 null 以及检查 String 中的空白空间,因为即使我们在 String 中有一个空格 myString=" "; myString.isEmpty() 将返回 false 。所以请考虑上面的情况,我会建议:

  if (myString != null && myString.trim().length() != 0 ) 
               return true;

回答by Pete

There's a StringUtils class in the apache.commons package that has lots of useful methods, like

apache.commons 包中有一个 StringUtils 类,它有很多有用的方法,比如

StringUtils.isEmpty(theString)

Which will do both comparisons, != null and isEmpty and looks better.

这将进行两种比较, != null 和 isEmpty 并且看起来更好。

回答by Subhrajyoti Majumder

Check Null also.

也检查 Null。

if( Index2!=null && !Index2.toString().isEmpty()){
  ....
}

回答by Ravi A

isEmpty()and nullhas nothing to do with each other. First checks the value of String if its equal to ""- OR you can say it will return true if its length is only and only 0. And second is checking for its initialization.

isEmpty()null彼此无关。首先检查 String 的值是否等于""- 或者你可以说如果它的长度只有并且只有 0,它将返回 true。第二个是检查它的初始化。

You should check for both of these condition if needed, like this,

如果需要,您应该检查这两种情况,如下所示,

if( str != null && !str.isEmpty() ) { ... }

if( str != null && !str.isEmpty() ) { ... }

OR

或者

if( str != null && !str.equals("") ) { ... }

if( str != null && !str.equals("") ) { ... }

OR use this in your case,

或者在你的情况下使用这个,

if(index2 != null) { if( index2.toString() != null && !index2.toString().isEmpty() ) { ... } }

if(index2 != null) { if( index2.toString() != null && !index2.toString().isEmpty() ) { ... } }

回答by anubhava

This is how you should check empty for your case:

这是您应该如何为您的案例检查空:

 if( Index2 == null || Index2.toString() == null || 
     Index2.toString().length() == 0)
     // it is empty

回答by Sumit Singh

Use The following code:

使用以下代码:

if(Index2!=null){
  if( !Index2.toString().isEmpty()){
    ....
  }
}