Java:字符串:equalsIgnoreCase 与将所有内容切换为大写/小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4446643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: String: equalsIgnoreCase vs switching everything to Upper/Lower Case
提问by Jason Rogers
It came to my attention that there a several ways to compare strings in Java.
我注意到在 Java 中有几种比较字符串的方法。
I just got in the habit ages ago to use equalsIgnoreCase
to avoid having problems with case sensitive strings.
我很久以前就养成了使用习惯equalsIgnoreCase
来避免区分大小写字符串的问题。
Others on the other hand prefer passing everything in upper or lower case.
另一方面,其他人更喜欢以大写或小写传递所有内容。
From where I stand (even if technically I'm sitting), I don't see a real difference.
从我站的位置(即使从技术上讲我是坐着的),我看不出真正的区别。
Does anybody know if one practice is better than the other? And if so why?
有人知道一种做法是否比另一种更好吗?如果是这样,为什么?
采纳答案by Asaph
Use equalsIgnoreCase
because it's more readable than converting both Strings to uppercase before a comparison. Readability trumps micro-optimization.
使用equalsIgnoreCase
是因为它比在比较之前将两个字符串都转换为大写更具可读性。可读性胜过微观优化。
What's more readable?
什么更具可读性?
if (myString.toUpperCase().equals(myOtherString.toUpperCase())) {
or
或者
if (myString.equalsIgnoreCase(myOtherString)) {
I think we can all agree that equalsIgnoreCase
is more readable.
我想我们都同意这equalsIgnoreCase
更具可读性。
回答by rkg
But the issue in the latter, where you make an assumptionthat either upper or lower case is passed, you cannot blindly trust the caller. So you have to include an ASSERT
statement at the start of the method to make sure that the input is always in the case your are expecting.
但是在后者的问题中,您假设通过了大写或小写,您不能盲目地相信调用者。因此,您必须ASSERT
在方法的开头包含一个语句,以确保输入始终符合您的预期。
回答by CurtainDog
Neither is better, they both have their uses in different scenarios.
两者都不是更好,它们都在不同的场景中使用。
Many times when you have to do string comparisons there is the opportunity to massage at least one of the strings to make it easier to compare, and in these cases you will see strings converted to a particular case, trimmed, etc before being compared.
很多时候,当您必须进行字符串比较时,有机会至少调整一个字符串以使其更容易进行比较,在这些情况下,您会看到字符串在比较之前转换为特定大小写、修剪等。
If, on the other hand, you just want to do an on-the-fly case-insensitive comparison of two strings then feel free to use equalsIgnoreCase
, that's what its there for after all. I would caution, however, that if you're seeing a lot of equalsIgnoreCase
it could be a code smell.
另一方面,如果您只想对两个字符串进行即时的不区分大小写的比较,则可以随意使用equalsIgnoreCase
,毕竟这就是它的用途。但是,我会警告说,如果您看到很多,equalsIgnoreCase
则可能是代码异味。
回答by Sileria
Performance wise both are same according to this post:
根据这篇文章,两者的性能都相同:
http://www.params.me/2011/03/stringtolowercasestringtouppercase-vs.html
http://www.params.me/2011/03/stringtolowercasestringtouppercase-vs.html
So I would decide based on code readabilty, in some case toLowerCase() would be better if I am passing a value always to a single method to create objects, otherwise equalsIgnoreCase() makes more sense.
所以我会根据代码可读性来决定,在某些情况下,如果我总是将一个值传递给一个方法来创建对象,toLowerCase() 会更好,否则 equalsIgnoreCase() 更有意义。
回答by koljaTM
equalsIgnoreCase avoids problems regarding Locale-specific differences (e.g. in Turkish Locale there are two different uppercase "i" letters). On the other hand, Maps only use the equals() method.
equalsIgnoreCase 避免了有关特定于区域设置的差异的问题(例如,在土耳其语区域设置中有两个不同的大写“i”字母)。另一方面,Maps 只使用 equals() 方法。
回答by 4castle
When I'm working with English-only characters, I always run toUpperCase()
or toLowerCase()
before I start doing comparisons if I'm calling .equalsIgnoreCase()
more than onceor if I'm using a switch
statement. This way it does the case-change operation only once, and so is more efficient.
当我使用纯英文字符时,如果我多次调用或使用语句,我总是在开始比较之前运行toUpperCase()
或运行。这样它只执行一次大小写更改操作,因此效率更高。toLowerCase()
.equalsIgnoreCase()
switch
For example, in a factory pattern:
例如,在工厂模式中:
public static SuperObject objectFactory(String objectName) {
switch(objectName.toUpperCase()) {
case "OBJECT1":
return new SubObject1();
break;
case "OBJECT2":
return new SubObject2();
break;
case "OBJECT3":
return new SubObject3();
break;
}
return null;
}
(Using a switch
statement is slightly faster than if..else if..else
blocks for String comparison)
(在字符串比较中,使用switch
语句比if..else if..else
块稍微快一些)
回答by Jim W
It depends on the use case.
这取决于用例。
If you're doing a one to one string comparison, equalsIgnoreCase is probably faster, since internally it just uppercases each character as it iterates through the strings (below code is from java.lang.String), which is slightly faster than uppercasing or lowercasing them all before performing the same comparison:
如果您正在进行一对一的字符串比较,equalsIgnoreCase 可能会更快,因为在内部它只是在遍历字符串时将每个字符大写(下面的代码来自 java.lang.String),这比大写或小写略快在执行相同的比较之前,它们全部:
if (ignoreCase)
{
// If characters don't match but case may be ignored,
// try converting both characters to uppercase.
// If the results match, then the comparison scan should
// continue.
char u1 = Character.toUpperCase(c1);
char u2 = Character.toUpperCase(c2);
if (u1 == u2) {
continue;
}
// Unfortunately, conversion to uppercase does not work properly
// for the Georgian alphabet, which has strange rules about case
// conversion. So we need to make one last check before
// exiting.
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
continue;
}
}
But when you have a situation where you want to do lookups against a data structure full of strings (especially strings that are all in the US Latin/ASCII space) in a case insensitive manner, it will be quicker to trim/lowercase the strings to be checked against and put them in something like a HashSet or HashMap.
但是,当您希望以不区分大小写的方式对充满字符串的数据结构(尤其是所有在美国拉丁/ASCII 空间中的字符串)进行查找时,将字符串修剪/小写会更快检查并将它们放入 HashSet 或 HashMap 之类的东西中。
This is better than calling equalsIgnoreCase on each element of a List because the slight performance gain of equalsIgnoreCase() is canceled out by the fact that you're basically doing a modified version of contains() against an array, which is O(n). With a pre-normalized string you can check against the entire list of strings with a single contains() call that runs in O(1).
这比在 List 的每个元素上调用 equalsIgnoreCase 更好,因为 equalsIgnoreCase() 的轻微性能增益被您基本上对数组执行 contains() 的修改版本这一事实抵消了,这是 O(n) . 使用预先规范化的字符串,您可以使用在 O(1) 中运行的单个 contains() 调用检查整个字符串列表。
回答by andrew dibiasio
equalsIgnoreCase documentation in jdk 8
jdk 8 中的 equalsIgnoreCase 文档
Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.
Two characters c1 and c2 are considered the same ignoring case if at least one of the following is true:
- The two characters are the same (as compared by the == operator)
- Applying the method java.lang.CharactertoUpperCase(char)to each character produces the same result
- Applying the method java.lang.CharactertoLowerCase(char) to each character produces the same result
将此字符串与另一个字符串进行比较,忽略大小写考虑。如果两个字符串的长度相同并且两个字符串中对应的字符是相等的忽略大小写,则认为两个字符串是相等的忽略大小写。
如果以下至少一项为真,则两个字符 c1 和 c2 被认为是相同的忽略大小写:
- 两个字符相同(由 == 运算符比较)
- 将方法 java.lang.CharactertoUpperCase(char) 应用于每个字符会产生相同的结果
- 将方法 java.lang.CharactertoLowerCase(char) 应用于每个字符会产生相同的结果
My thoughts:
我的想法:
So using equalsIgnoreCase we iterate through the Strings (only if their size values are the same) comparing each char. In the worst case, we will performance will be O( 3cn ) where n = the size of your strings. We will use no extra space.
因此,使用 equalsIgnoreCase 我们遍历字符串(仅当它们的大小值相同时)比较每个字符。在最坏的情况下,我们的性能将是 O( 3cn ),其中 n = 字符串的大小。我们不会使用额外的空间。
Using toUpper() then comparing if the strings are equal, you ALWAYS loop through each string one time, converting all strings to upper, then do an equivalence by reference check (equals()). This is theta(2n + c). But just remember, when you do toUpperCase(), you actually have to create two new Strings because Strings in Java are immutable.
使用 toUpper() 然后比较字符串是否相等,您总是循环遍历每个字符串一次,将所有字符串转换为 upper,然后通过引用检查(equals())进行等效。这是 theta(2n + c)。但是请记住,当您执行 toUpperCase() 时,您实际上必须创建两个新字符串,因为 Java 中的字符串是不可变的。
So I would say that equalsIgnoreCase is both more efficient and easier to read.
所以我会说 equalsIgnoreCase 更有效也更容易阅读。
Again I would consider the use case, because that would be what it comes down to for me. The toUpper approach could be valid in certain use cases, but 98% of the time I use equalsIgnoreCase().
我会再次考虑用例,因为这对我来说就是它。toUpper 方法在某些用例中可能有效,但 98% 的时间我都使用 equalsIgnoreCase()。