比较Android上的两个字符串

时间:2011-04-22 08:11:27

标签: android

在if-else条件下比较两个字符串时,执行错误的分支。我有s1 = "failure"并将其与ss进行比较以执行,但它会转到else分支。为什么,当条件满足时?

public class cabbookingconfirmation extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cabbbookingconfirmation);
        String ss = Activity1.getData();
        String s1 = "failure";
        Log.i("tag..........", ss);
        if (ss.equals(s1)) {
             Log.i("saurabh..........","trivedi....");
        } else {
            TextView tv = (TextView)findViewById(R.id.details);
            tv.setText("Dear Gaurav....," +
                "Your cab has been booked. " +
                "Please refer to your planner " +
                "for details.Have a safe trip!");
            tv.setBackgroundColor(111);
        }
    }
}

activity1.java位于

之下
public class Activity1 extends Activity
{
    public static String getData() {
        String responceid = null;
        try {
            URL url = new URL("http://qrrency.com/mobile/j2me/cab/BookCab.php?bookingid=666");

            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            int m = 0;
            StringBuffer buffer = new StringBuffer();
            String str1 = " ";
            while ((m = in.read()) != -1)
            {
                       buffer.append((char)m);
                       str1 = str1 + (char)m;
                       responceid = str1;
            }

            Log.i("Line----saurabh trivedi---,------,,--", responceid);

            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return responceid;
    }
}

1 个答案:

答案 0 :(得分:-1)

我真的不知道Andriod SDK

<。>在.net

 if (ss.equals(s1))

应该完美!也许在这里ss.equals不测试字符串?或者返回的字符串是另一种格式还是空格?

尝试并使用

(string1.equalsIgnoreCase(string2))

edit-removed(ss == s1) - 不正确的原始答案

这是java语法,应该可以工作 - 如果没有..那么在两个蜇中有一些不同的东西,你必须解码它们,看看差异在哪里

顺便说一句,这是做什么的

  Log.i("tag..........",ss);

你确定不会以某种方式影响ss吗?它在宣言之前和之后的权利