Android Studio Java String Compare

拥有回忆 提交于 2021-02-17 05:53:05

问题


I have string to compare but I can't make it to work even I searched google and stackoverflow.

So here is my code:

@Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            if(s.equalsIgnoreCase("success")){
                Intent intent = new Intent(Najava.this,UserProfile.class);
                intent.putExtra(USER_NAME,username);
                startActivity(intent);
            }else{
                Toast.makeText(Najava.this,s,Toast.LENGTH_LONG).show();
            }
        }

But the activity never changes and the toast message shows up with the same string: http://i.stack.imgur.com/ApJLp.png


回答1:


It seems that String s has un-necessary characters present. Try below:

if(s.contains("success"){}

Note: For string comparison, its always good practice to compare constant value with other string. This will save code from Null pointers.

if("success".equalsIgnoreCase(s)){}



回答2:


Put this line before your if:

s = s.toLowerCase().trim();

or remove s from super.onPostExecute();



来源:https://stackoverflow.com/questions/32026016/android-studio-java-string-compare

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!