java 如何在 Android Studio 中检查字符串是否为空或 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37269219/
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
how to check if a string is empty or null in Android Studio
提问by John Champion
In my project I have an edit text
field and its name is editText
also I have assign mTextEmri = (EditText) findViewById(R.id.editText);
I want to get its value and I have done it with the following statment String emridhene = mTextEmri.getText().toString();
在我的项目中,我有一个edit text
字段,它的名称editText
也是我指定的mTextEmri = (EditText) findViewById(R.id.editText);
我想获取它的值,我已经使用以下语句完成了String emridhene = mTextEmri.getText().toString();
Now I want to check it if it is null
or empty
I have tried this code but it is not working
现在我想检查它是否是null
或者empty
我已经尝试过这段代码但它不起作用
if (emridhene.toString() == null || emridhene.toString() == "") {
mTextViewKonfirm.setText("Ju lutem shkruani nje Emer");
}
else{
mTextViewKonfirm.setText(emridhene);
}
How can I make it to work. Thanks.
我怎样才能让它工作。谢谢。
回答by Deepak Goyal
Just Use
就用
if(TextUtils.isEmpty(/* your String*/)){
// String is empty or null
}else {
// string has value
}