Android check null or empty string in Android

前端 未结 10 1501
梦如初夏
梦如初夏 2020-12-13 12:07

In my trying AsyncTask I get email address from my server. In onPostExecute() I have to check is email address empty or null

10条回答
  •  旧巷少年郎
    2020-12-13 12:38

    Use TextUtils.isEmpty( someString )

    String myString = null;
    
    if (TextUtils.isEmpty(myString)) {
        return; // or break, continue, throw
    }
    
    // myString is neither null nor empty if this point is reached
    Log.i("TAG", myString);
    

    Notes

    • The documentation states that both null and zero length are checked for. No need to reinvent the wheel here.
    • A good practice to follow is early return.

提交回复
热议问题