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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:22:01  来源:igfitidea点击:

how to check if a string is empty or null in Android Studio

javaandroidandroid-layoutandroid-studio

提问by John Champion

In my project I have an edit textfield and its name is editTextalso 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 nullor emptyI 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
}