简体   繁体   English

Android Studio Java 字符串比较

[英]Android Studio Java String Compare

I have string to compare but I can't make it to work even I searched google and stackoverflow.我有要比较的字符串,但即使我搜索了 google 和 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但活动永远不会改变,吐司消息以相同的字符串显示: http : //i.stack.imgur.com/ApJLp.png

It seems that String s has un-necessary characters present.似乎 String s 存在不必要的字符。 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)){}

Put this line before your if:将此行放在您的 if 之前:

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

or remove s from super.onPostExecute();或从super.onPostExecute();删除 s super.onPostExecute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM