2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-01 15:06:48 +02:00
Using `toLowerCase()` or `toUpperCase()` to make case insensitive comparisons is inefficient because it requires the creation of temporary, intermediate `String` objects.
2021-04-28 16:49:39 +02:00
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-06-09 16:36:32 +02:00
[source,java,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
2023-06-01 15:06:48 +02:00
private void compareStrings(String foo, String bar){
boolean result1 = foo.toUpperCase().equals(bar); // Noncompliant
boolean result2 = foo.equals(bar.toUpperCase()); // Noncompliant
boolean result3 = foo.toLowerCase().equals(bar.toLowerCase()); // Noncompliant
}
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2023-06-09 16:36:32 +02:00
[source,java,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
2023-06-01 15:06:48 +02:00
private void compareStrings(String foo, String bar){
2023-06-09 16:36:32 +02:00
boolean result1 = foo.equalsIgnoreCase(bar); // Compliant
2023-06-01 15:06:48 +02:00
}
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Exceptions
2021-04-28 16:49:39 +02:00
2023-06-01 15:06:48 +02:00
No issue will be raised when a locale is specified because the result could be different from `equalsIgnoreCase()`. (e.g.: using the Turkish locale)
2021-04-28 16:49:39 +02:00
2023-05-25 14:18:12 +02:00
[source,java]
2021-04-28 16:49:39 +02:00
----
2023-06-01 15:06:48 +02:00
private void compareStrings(String foo, String bar, java.util.Locale locale){
boolean result1 = foo.toUpperCase(locale).equals(bar); // Compliant
}
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Replace "[toUpperCase()|toLowerCase()]" and "equals()" calls with a single call to "equalsIgnoreCase()".
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 29 Jul 2013, 14:10:11 Dinesh Bolkensteyn wrote:
This one is interesting, I made the mistake a few times I think ;-)
=== on 29 Jul 2013, 14:10:24 Dinesh Bolkensteyn wrote:
Implemented by \http://jira.codehaus.org/browse/SONARJAVA-245
=== on 29 Jul 2013, 23:56:51 Ann Campbell wrote:
Dinesh went a lot of rounds on this one. It'll probably kill him that I still made a change.
=== on 30 Jul 2013, 08:21:27 Dinesh Bolkensteyn wrote:
Not at all Ann ;-) I didn't yet commit the implementation, so your change did not take any additional effort to apply! Thanks!
=== on 31 Jul 2013, 00:10:00 Ann Campbell wrote:
Sorry Dinesh, but I missed a spot yesterday. :-(
=== on 7 Aug 2013, 05:52:30 Dinesh Bolkensteyn wrote:
Thanks, I've just applied your changes!
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]