字符串比较问题
我正在尝试做一些非常简单的事情,但在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
等于
确实有效。如果temp.equals("Location")
返回 false,则您的temp
变量不会引用值为“Location”的字符串。该字符串可能存在无法打印的字符或其他奇怪的情况 - 我建议您查看该字符串的长度进行检查。或者,可能还有其他看起来类似于 ASCII 字符的字符,但实际上并非如此。在调试器中,尝试检查数组并获取底层 char 数组 - 检查每个字符的 Unicode 值。
equals
does work. Iftemp.equals("Location")
returns false, then yourtemp
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.
尝试像
和
try like
and
如果您的变量 temp 是
String
,您还可以使用compareTo(String)
方法。if your variable temp is a
String
, you can also used the methodcompareTo(String)
.我正在做同样的场景,它工作正常。
I am doing same scenario , its working fine.
尝试这样做:
Try doing this:
首先尝试将“temp”转换为字符串然后比较它,应用这可能会帮助你
first try to convert "temp" into string then compare it, apply this may helps you
您可以尝试以下方法来找出您的问题所在。
这应该将两个字符串的字节表示形式打印到标准输出。如果 equals() 返回 false,则字符串不同。由于无法打印的字符或外观相似的字符,有时很难找到差异。但字节表示应该告诉你。
(我不是 Android 程序员,所以我希望这些函数存在于 Android JVM 上。对于任何拼写错误和缺少括号 - 如果有的话,我们深表歉意;-)
you may try the following to find out where your problem is.
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 ;-)