[英]String comparison in Java
I want to compare two strings using Java. 我想使用Java比较两个字符串。 First sting
name
i get from .mif
file using GDAL in cp1251 encoding. 我使用cp1251编码的GDAL从
.mif
文件获得的第一个字符串name
。 Second kadname
i get from jsp. 我从jsp获得的第二个
kadname
。 To compare i do this: 为了比较我这样做:
if (attrValue instanceof String)
{
String string3 =
new String((attrValue.toString()).getBytes("ISO-8859-1"), "cp1251");
dbFeature.setAttribute(name, string3);
System.out.println("Name=" + name);
System.out.println("kadname=" + kadname);
if (name.equalsIgnoreCase(kadname))
{
kadnum = string3;
System.out.println("string3" + string3);
}
}
And in console i get this: 在控制台中,我得到以下信息:
Name = kadnumm
kadname = kadnumm
Whats wrong with this? 这怎么了
The only reason I can see for them not being equal is the leading or trailing whitespace. 我看到它们不相等的唯一原因是前导或尾随空格。
You can trim
the string to remove any of those whitespaces, and then compare them: - 您可以
trim
字符串以删除所有这些空格,然后进行比较:-
if (name.trim().equalsIgnoreCase(kadname.trim()))
Or, there might be some other non-printable
characters, which won't get removed by trimming
. 或者,可能还有其他一些
non-printable
字符,这些字符不会被trimming
删除。 You can try printing your strings in single quotes
, and check whether there are any: - 您可以尝试使用
single quotes
将字符串打印出来,并检查是否有以下内容:-
System.out.println("'" + name + "'");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.