在Java中检查空字符串

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

Checking empty string in Java

javastring

提问by Surjya Narayana Padhi

I don't know what is wrong with the below code.... I am getting input from a textbox and putting the input in a string. If the textbox is empty it will return a empty string. In the below code

我不知道下面的代码有什么问题....我从文本框中获取输入并将输入放入字符串中。如果文本框为空,它将返回一个空字符串。在下面的代码中

   String[] str = new String[9]; 
   for(int i=0;i<9;i++){
       if(str[i].equals("")){
          System.out.println("set " + cmds[i] + " " + str[i]);
          WriteThread.sendCommand("set " + cmds[i] + " " + str[i] + "\n", false);
       }
    }

In the above code str[i]stores the input text of textboxes and I am trying to check if any element of the array is empty. I also tried with str[i] == ""and str[i] == nullbut no luck. The statement inside the if block if i am printing the string str[i], it shows nothing that means it is empty.

在上面的代码中str[i]存储文本框的输入文本,我试图检查数组的任何元素是否为空。我也尝试过str[i] == ""str[i] == null但没有运气。if 块中的语句如果我正在打印字符串str[i],它不显示任何表示它为空的内容。

Am I doing anything the wrong way?

我做错了什么吗?

采纳答案by fastcodejava

You could try :

你可以试试:

if (str[i] == null || str[i].trim().equals("")){
// your code
}

回答by GrayWizardx

The above code only executes if the string is empty, is that the desired behavior? Also declaring str as new right before the loop pretty much guarantees it will be empty.

上面的代码仅在字符串为空时才执行,这是期望的行为吗?同样在循环之前将 str 声明为 new 几乎可以保证它是空的。

If you are trying to assign the values from textboxes you will need to have those assignments somewhere before the loop.

如果您尝试从文本框中分配值,则需要在循环之前的某处进行这些分配。

回答by Romain Linsolas

You can use the Apache Commons Langto check a String:

您可以使用Apache Commons Lang检查字符串:

if (StringUtils.isBlank(str[i]) {
    ...
}

StringUtils.isBlankis checking if the String is nullor empty (i.e. if it is equals to ""when all blank characters are removed).

StringUtils.isBlank正在检查字符串是否为null空(即是否等于""删除所有空白字符时)。

回答by algernon

I actually tried to run your code snippet and got a NullPointerException on your conditional

我实际上试图运行你的代码片段并在你的条件下得到一个 NullPointerException

str[i].equals("")

str[i].equals("")

str[i] == null worked for me. Are you sure you put the right code in your question. It seems odd to be testing for an empty string of an empty array you just initialized.

str[i] == null 对我有用。您确定您在问题中输入了正确的代码。测试您刚刚初始化的空数组的空字符串似乎很奇怪。

回答by pardeep131085

I would recommend

我会推荐

if (str[i] == null || str[i].trim().length==0){
// your code
}

or Java 6 onward

或 Java 6 以上

if (str[i] == null || str[i].trim().isEmpty()){
// your code
}

Instead of using equals() because equals() method is overkill to test for an empty String.

而不是使用 equals() 因为 equals()方法测试空 String 是过度的

回答by Chasmatu

A very simple solution can be like this.

一个非常简单的解决方案可以是这样的。

if(str[] != null && !str[].equals(""))