【问题标题】:String comparison issue字符串比较问题
【发布时间】:2011-08-08 06:36:46
【问题描述】:

我正在尝试做一些非常简单但在 android 中显然非常困难的事情。 我只是想比较两个字符串,看看它们是否相等。

我有一个值为“位置”的临时变量 我已经调试过了,它确实包含位置... 所以我一开始就尝试了这个

if(temp == "Location") { //do something }

但我已经知道这行不通。然后我尝试了所有可能的字符串函数,例如:

.等于 .包含 .ignoreCaseEquals 等等……

如果有人知道该怎么做,请帮忙。这真的很烦人。

编辑: 这是我为想要查看的人比较字符串的函数。

public String[] getData(){ 
    try {
        int tempGroupCount = 0;
        URL food_url = new URL (Constants.SERVER_DINING);
        BufferedReader my_buffer = new BufferedReader(new InputStreamReader(food_url.openStream()));
        temp = my_buffer.readLine();
        // prime read
        while (temp != null ){
            // check to see if readline equals Location
            Log.w("HERasdfsafdsafdsafE", temp);
            // start a new location
            if (temp.equals("Location")
            {
                groups[tempGroupCount] = temp;
                tempGroupCount++;
            }
                Log.w("HERasdfsafdsafdsafE", temp);
                //start for-loop to test to get child info
                //for(temp = my_buffer.readLine(); temp != "Location" && temp != null; groupCount++, childrenCount++){
                    //children[groupCount][childrenCount] = temp;
                //}

            temp = my_buffer.readLine();
        }
        my_buffer.close();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("IO EXCEPTION", "Exception occured in MyExpandableListAdapter:" + e.toString());
    }
    return groups;
    }

【问题讨论】:

标签: java android string compare


【解决方案1】:

equals 确实工作。如果temp.equals("Location") 返回false,那么您的temp 变量不引用值为“Location”的字符串。

字符串可能有不可打印的字符或其他奇怪的东西 - 我建议您查看要检查的字符串的 长度。或者,可能有其他字符 看起来 像 ASCII 字符,但不是。在调试器中,尝试检查数组并获取底层 char 数组 - 检查每个字符的 Unicode 值。

【讨论】:

  • 好吧,我从中提取的数据位于服务器 mtweb.mtsu.edu/mobiledev2/Dining/Food_Times.txt 上,我无权编辑服务器 atm 上的文件,因此我无法检查是否有问题文本文件在空格中可能会抛出它。当您在调试器中说时,我可以将每个字符串数组视为字符...如何?
  • Android 有一个很棒的调试器。如果您不想使用它,您可以简单地遍历每个字符(通过length() 获取大小)并通过charAt 将Unicode 值打印到通过Log.d() 的调试输出。
  • @cj1098:你说你已经调试了代码——所以大概你知道如何使用调试器。当你检查一个字符串时,你应该能够得到一个更“原始”的视图,这将包括对底层 char 数组的访问。
【解决方案2】:
if(temp.equals("Location"))
{
         //your code here
}
does not work

try this
if(temp.contains("Location"))
{
         //your code here
}

【讨论】:

  • temp 不是一个集合,它是一个字符串变量。
【解决方案3】:

试试看

if(temp.equals("Location")) { //do something }

和

while (!temp.equals("")){

【讨论】:

    【解决方案4】:

    如果你的变量 temp 是 String,你也可以使用方法 compareTo(String)。

    if (temp.compareTo("Location") == 0) 
    { 
        //do something 
    }
    

    【讨论】:

      【解决方案5】:

      我正在做同样的场景,它工作正常。

          String result = responsePrimitiveData.toString();
      if(!result.equals("0")){ 
           }
      

      【讨论】:

        【解决方案6】:

        尝试这样做:

        if (temp.toLowerCase().compareTo("location") == 0)
        

        【讨论】:

          【解决方案7】:
          public String[] getData(){ 
              try {
                  int tempGroupCount = 0;
                  URL food_url = new URL (Constants.SERVER_DINING);
                  BufferedReader my_buffer = new BufferedReader(new InputStreamReader(food_url.openStream()));
                  temp = my_buffer.readLine();
                  // prime read
                  while (temp != null ){
                      // check to see if readline equals Location
                      Log.w("HERasdfsafdsafdsafE", temp);
                      // start a new location
                      if (temp.toString().equalsIgnoreCase("Location")
                      {
                          groups[tempGroupCount] = temp;
                          tempGroupCount++;
                      }
                          Log.w("HERasdfsafdsafdsafE", temp);
                          //start for-loop to test to get child info
                          //for(temp = my_buffer.readLine(); temp != "Location" && temp != null; groupCount++, childrenCount++){
                              //children[groupCount][childrenCount] = temp;
                          //}
          
                      temp = my_buffer.readLine();
                  }
                  my_buffer.close();
              } catch (MalformedURLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  Log.e("IO EXCEPTION", "Exception occured in MyExpandableListAdapter:" + e.toString());
              }
              return groups;
              }
          

          首先尝试将“temp”转换为字符串然后比较它,应用它可能对你有帮助

          【讨论】:

            【解决方案8】:

            您可以尝试以下方法来找出您的问题所在。

            final String LOCATION = "Location"; // just to make sure we use the very same character sequence
            
            if (temp.equals(LOCATION)
            {
                /* your code here */
            }
            else
            {
                System.out.println("Location : " + Arrays.toString(LOCATION.getBytes(Charset.forName("UTF-8"))));
                System.out.println("temp     : " + Arrays.toString(temp.getBytes(Charset.forName("UTF-8"))));
            }
            

            这应该将两个字符串的字节表示打印到标准输出。如果 equals() 返回 false,则字符串不同。由于无法打印的字符或外观相似的字符,有时很难找到差异。但是字节表示应该向您展示。

            (我不是 android 程序员,所以我希望这些函数存在于 android JVM 上。对于任何拼写错误和缺少括号表示抱歉 - 如果有的话 ;-)

            【讨论】:

              猜你喜欢
              • 2019-05-04
              • 2017-09-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-02-08
              • 2013-05-30
              相关资源
              最近更新 更多