简体   繁体   English

Android字符串比较不起作用

[英]Android String Comparison Not Working

I am having hard time comparing two strings in Android using Java, what I am doing is running HTTP get request which return yes or no and based on that I decide whether to launch a new activity or not. 我很难用Android比较Android中的两个字符串,我正在做的是运行HTTP get请求,返回yes或no,并根据我决定是否启动新活动。

I am performing the string comparison inside Async onPostExecute method and though the HTTP returned string is yes it is not working for me. 我在Async onPostExecute方法中执行字符串比较,虽然HTTP返回的字符串是yes但它对我不起作用。

Here goes the code 这是代码

        protected void onPostExecute(String result) {
        progressDialog.dismiss();
        if(result.equals("yes"))
        {
            Intent toDashboard = new Intent(LoginActivity.this,Dashboard.class); 
            startActivity(toDashboard);
            Toast.makeText(LoginActivity.this, "It was yes", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(LoginActivity.this, result, Toast.LENGTH_LONG).show();
        }
    }

Though the returned string is yes it still skips the if statement and creates a toast showing "Yes". 虽然返回的字符串为yes,但它仍会跳过if语句并创建一个显示“Yes”的toast。

I have also tried some other ways to match a string and they all goes here 我还尝试了一些匹配字符串的其他方法,它们都在这里

if(result == "yes")
if(result.toString().equals("yes"))
if(result.contentEquals("yes"))
if(result.equalsIgnoreCase("yes"))

None of them works for me 它们都不适合我

There might be some white space in result. 结果可能会有一些空白区域。

Try trimming it. 尝试修剪它。

result.trim().equals("yes")

try this 尝试这个

str1.toLowerCase().contains(str2.toLowerCase()) str1.toLowerCase()。含有(str2.toLowerCase())

it work for me 它对我有用

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

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