字符串比较问题

发布于 2024-11-28 22:26:42 字数 1702 浏览 1 评论 0原文

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

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

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

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

.equals .包含 .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;
    }

I'm trying to do something reallllly simple that apparently is extremely difficult in android.
I just want to compare two strings to see if they are equal.

I have a temp variable with the value "Location"
I have debugged this and it does indeed contain Location...
So I tried this at first

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

But I already know that doesn't work. I then tried all the possible functions for a string such as:

.equals
.contains
.ignoreCaseEquals
etc...

If anyone has any idea what to do please help. This is really getting annoying.

EDIT:
Here is the function where I'm comparing the strings for those of you who want to see.

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

等于 确实有效。如果 temp.equals("Location") 返回 false,则您的 temp 变量不会引用值为“Location”的字符串。

该字符串可能存在无法打印的字符或其他奇怪的情况 - 我建议您查看该字符串的长度进行检查。或者,可能还有其他看起来类似于 ASCII 字符的字符,但实际上并非如此。在调试器中,尝试检查数组并获取底层 char 数组 - 检查每个字符的 Unicode 值。

equals does work. If temp.equals("Location") returns false, then your temp variable does not refer to a string with the value "Location".

There may be unprintable characters or other oddities about the string - I suggest you look at the length of the string to check. Alternatively, there can be other characters which look like the ASCII characters, but aren't. In the debugger, try examining the array and get at the underlying char array - check the Unicode value of each character.

回梦 2024-12-05 22:26:42
if(temp.equals("Location"))
{
         //your code here
}
does not work

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

try this
if(temp.contains("Location"))
{
         //your code here
}
笨笨の傻瓜 2024-12-05 22:26:42

尝试像

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

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

try like

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

and

while (!temp.equals("")){
§普罗旺斯的薰衣草 2024-12-05 22:26:42

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

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

if your variable temp is a String, you can also used the method compareTo(String).

if (temp.compareTo("Location") == 0) 
{ 
    //do something 
}
花开浅夏 2024-12-05 22:26:42

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

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

I am doing same scenario , its working fine.

    String result = responsePrimitiveData.toString();
if(!result.equals("0")){ 
     }
一刻暧昧 2024-12-05 22:26:42

尝试这样做:

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

Try doing this:

if (temp.toLowerCase().compareTo("location") == 0)
指尖上得阳光 2024-12-05 22:26:42
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”转换为字符串然后比较它,应用这可能会帮助你

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;
    }

first try to convert "temp" into string then compare it, apply this may helps you

甜柠檬 2024-12-05 22:26:42

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

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 上。对于任何拼写错误和缺少括号 - 如果有的话,我们深表歉意;-)

you may try the following to find out where your problem is.

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"))));
}

This should print the byte representation of both Strings to standard out. If equals() returns false, the strings differ. Because of unprintable characters or similar looking characters it's sometimes difficult to find the difference. But the byte representation should show you.

(I'm not an android programmer, so I hope the functions exist on android JVM. And sorry for any typos and missing brackets - if any ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文