在Java中检查空字符串 [英] Checking empty string in Java
本文介绍了在Java中检查空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道下面的代码有什么问题....我从文本框中获取输入并将输入放在一个字符串中。如果文本框为空,则返回空字符串。在下面的代码中
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);
}
}
在上面的代码中 str [i]
存储文本框的输入文本,我试图检查数组的任何元素是否为空。我也试过 str [i] ==
和 str [i] == null
但没有运气。 if块中的语句,如果我打印字符串 str [i]
,它显示没有任何意味着它是空的。
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] == null
but no luck. The statement inside the if block if i am printing the string str[i]
, it shows nothing that means it is empty.
我做错了什么?
推荐答案
你可以尝试:
if (str[i] == null || str[i].trim().equals("")){
// your code
}
这篇关于在Java中检查空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文