如何使字符串比较不区分大小写?
我创建了一个 Java 程序来比较两个字符串:
String str = "Hello";
if (str.equals("hello")) {
System.out.println("match");
} else {
System.out.println("no match");
}
它区分大小写。我怎样才能改变它,使它不再是这样?
I created a Java program to compare two strings:
String str = "Hello";
if (str.equals("hello")) {
System.out.println("match");
} else {
System.out.println("no match");
}
It's case-sensitive. How can I change it so that it's not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
最好的方法是使用
str.equalsIgnoreCase("foo")
。它专门为此目的进行了优化。您还可以在与
equals
进行比较之前将这两个字符串转换为大写或小写。对于可能没有等价的equalsIgnoreCase
的其他语言来说,记住这个技巧很有用。如果您使用非罗马字母,请注意 JavaDoc of equalsIgnoreCase 的这一部分,其中表示
The best way is to use
str.equalsIgnoreCase("foo")
. It's optimized specifically for this purpose.You can also convert both strings to upper- or lowercase before comparing them with
equals
. This is a trick that's useful to remember for other languages which might not have an equivalent ofequalsIgnoreCase
.If you are using a non-Roman alphabet, take note of this part of the JavaDoc of
equalsIgnoreCase
which says使用
String.equalsIgnoreCase()
。使用 Java API 参考来查找如下答案:
https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String)
< a href="https://docs.oracle.com/javase/1.5.0/docs/api/" rel="noreferrer">https://docs.oracle.com/javase/1.5.0/docs/api /
Use
String.equalsIgnoreCase()
.Use the Java API reference to find answers like these:
https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String)
https://docs.oracle.com/javase/1.5.0/docs/api/
< code>String.equalsIgnoreCase 是简单的不区分大小写字符串比较的最实用选择。
不过,值得注意的是,此方法既不进行完整大小写折叠,也不进行分解,因此无法执行 Unicode 标准中指定的无大小写匹配。事实上,JDK API 不提供对有关大小写折叠字符数据的信息,因此这项工作最好委托给经过尝试和测试的第三方库。
该库是 ICU,以下是如何实现一个实用程序不区分大小写的字符串比较:
即使是这个简单的测试,在大写或小写字符串上与
String.equalsIgnoreCase
或String.equals
进行简单比较也会失败。(请注意,预定义的大小写折叠风格
getNFKCCasefoldInstance
与语言环境无关;对于土耳其语语言环境,需要更多工作,涉及UCharacter.foldCase
可能是必要的。)String.equalsIgnoreCase
is the most practical choice for naive case-insensitive string comparison.However, it is good to be aware that this method does neither do full case folding nor decomposition and so cannot perform caseless matching as specified in the Unicode standard. In fact, the JDK APIs do not provide access to information about case folding character data, so this job is best delegated to a tried and tested third-party library.
That library is ICU, and here is how one could implement a utility for case-insensitive string comparison:
Naive comparison with
String.equalsIgnoreCase
, orString.equals
on upper- or lowercased strings will fail even this simple test.(Do note though that the predefined case folding flavour
getNFKCCasefoldInstance
is locale-independent; for Turkish locales a little more work involvingUCharacter.foldCase
may be necessary.)您必须使用
String
对象的compareToIgnoreCase
方法。if (compareValue == 0)
表示str1
等于str2
。You have to use the
compareToIgnoreCase
method of theString
object.if (compareValue == 0)
it meansstr1
equalsstr2
.现在它将输出:
hai
Now it will output :
hai
在默认的 Java API 中,您有:
因此,如果您要使用具有排序数据结构的字符串,则无需重写比较器。
是您想要在自己的代码中进行纯粹的相等检查的内容。
只是为了进一步了解有关 Java 中字符串相等性的任何信息。 java.lang.String 类的 hashCode() 函数“区分大小写”:
因此,如果您想使用以字符串作为键的 Hashtable/HashMap,并且具有像“SomeKey”、“SOMEKEY”和“somekey”这样的键,请执行以下操作:视为相等,那么您必须将字符串包装在另一个类中(您不能扩展 String,因为它是最终类)。例如:
然后这样使用它:
In the default Java API you have:
So you do not need to rewrite a comparator if you were to use strings with Sorted data structures.
Is what you want for pure equality checks in your own code.
Just to further informations about anything pertaining to equality of Strings in Java. The hashCode() function of the java.lang.String class "is case sensitive":
So if you want to use an Hashtable/HashMap with Strings as keys, and have keys like "SomeKey", "SOMEKEY" and "somekey" be seen as equal, then you will have to wrap your string in another class (you cannot extend String since it is a final class). For example :
and then use it as such:
请注意,在执行 .equals 或 .equalsIgnoreCase 之前,您可能还想对它们进行空检查。
null String 对象无法调用 equals 方法。
IE:
Note that you may want to do null checks on them as well prior to doing your .equals or .equalsIgnoreCase.
A null String object can not call an equals method.
ie:
使用 s1.equalsIgnoreCase(s2) : https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String) 。
Use
s1.equalsIgnoreCase(s2)
: https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String).您可以使用 等于忽略大小写
You can use equalsIgnoreCase
有关字符串的更多信息可以在 String Class< /a> 和 字符串教程
More about string can be found in String Class and String Tutorials
为了空安全,您可以使用
或
To be nullsafe, you can use
or