Android中的字符串相等

时间:2016-02-22 14:55:31

标签: java android

在我的Android应用中,我输入字符串“el”。在AsyncTask中,如果我输入“el”,我会做一些操作。在这段代码中:

protected class AsyncTranslator extends AsyncTask<String, JSONObject, String> {
    @Override
    protected String doInBackground(String... params) {

        String mymeaning = null;
        RestAPI api = new RestAPI();
        try {

            //if(params[0] == "el") this doesn't work
            //if(params[0].trim() == "el") this doesn't work either
          //  if(params[0].substring(0, 1).trim() == "e" && params[0].substring(params[0].length() -1).trim() == "l") this doesn't work either
         if(params[0].contains("el")) //this works but it works for other elements containing "el" as well

我应该写什么样的,以便它只能在字符串“el”上工作。感谢。

1 个答案:

答案 0 :(得分:2)

尝试:

if ("el".equals(params[0])) {
    // do your stuff
}