Is there a way to compare 2 Strings without it being case-sensitive? [duplicate]
This question already has an answer here:
How do I make my string comparison case insensitive?
12 answers
I've just spent almost hour debugging a List<String>
in my code which was being sorted wrong by my Comparator
.
Turns out that string.compareTo(string2)
is case-sensitive. Meaning that all Capital letters come before the lowercase letters. e.g. "Z" comes before "d".
Is there any better way of comparing 2 Strings
inside a Comparator
and sorting them alphabetically ascending without them being case sensitive other then string.toLowerCase().compareTo(string2.toLowerCase());
?
Edit: There's a possibility of any accented letter appearing in my String
like for example: ä, ö, ü, é, è, etc.
java comparator
marked as duplicate by hellow, chŝdk, SpaceTrucker, Stephen C
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 at 13:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 1 more comment
This question already has an answer here:
How do I make my string comparison case insensitive?
12 answers
I've just spent almost hour debugging a List<String>
in my code which was being sorted wrong by my Comparator
.
Turns out that string.compareTo(string2)
is case-sensitive. Meaning that all Capital letters come before the lowercase letters. e.g. "Z" comes before "d".
Is there any better way of comparing 2 Strings
inside a Comparator
and sorting them alphabetically ascending without them being case sensitive other then string.toLowerCase().compareTo(string2.toLowerCase());
?
Edit: There's a possibility of any accented letter appearing in my String
like for example: ä, ö, ü, é, è, etc.
java comparator
marked as duplicate by hellow, chŝdk, SpaceTrucker, Stephen C
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 at 13:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
use.equalsIgnoreCase()
– Sofo Gial
Nov 16 at 9:40
1
@SofoGial That checks equality only, not which is bigger or smaller whichcopareTo
does and which is required to establish sorting order.
– Pshemo
Nov 16 at 9:41
Do you use accented letters (or umlauts etc)?
– DodgyCodeException
Nov 16 at 9:47
@DodgyCodeException yes I do, sry for forgetting to Mention that. There's the possibility of accented letters existing like for example: ä, ö, ü, è, é, etc...
– D.Mendes
Nov 16 at 9:51
@D.Mendes While accented characters are handled by ignoring the case, it will assume a != ä but ä == Ä
– Peter Lawrey
Nov 16 at 9:53
|
show 1 more comment
This question already has an answer here:
How do I make my string comparison case insensitive?
12 answers
I've just spent almost hour debugging a List<String>
in my code which was being sorted wrong by my Comparator
.
Turns out that string.compareTo(string2)
is case-sensitive. Meaning that all Capital letters come before the lowercase letters. e.g. "Z" comes before "d".
Is there any better way of comparing 2 Strings
inside a Comparator
and sorting them alphabetically ascending without them being case sensitive other then string.toLowerCase().compareTo(string2.toLowerCase());
?
Edit: There's a possibility of any accented letter appearing in my String
like for example: ä, ö, ü, é, è, etc.
java comparator
This question already has an answer here:
How do I make my string comparison case insensitive?
12 answers
I've just spent almost hour debugging a List<String>
in my code which was being sorted wrong by my Comparator
.
Turns out that string.compareTo(string2)
is case-sensitive. Meaning that all Capital letters come before the lowercase letters. e.g. "Z" comes before "d".
Is there any better way of comparing 2 Strings
inside a Comparator
and sorting them alphabetically ascending without them being case sensitive other then string.toLowerCase().compareTo(string2.toLowerCase());
?
Edit: There's a possibility of any accented letter appearing in my String
like for example: ä, ö, ü, é, è, etc.
This question already has an answer here:
How do I make my string comparison case insensitive?
12 answers
java comparator
java comparator
edited Nov 16 at 9:53
asked Nov 16 at 9:38
D.Mendes
11810
11810
marked as duplicate by hellow, chŝdk, SpaceTrucker, Stephen C
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 at 13:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by hellow, chŝdk, SpaceTrucker, Stephen C
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 at 13:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
use.equalsIgnoreCase()
– Sofo Gial
Nov 16 at 9:40
1
@SofoGial That checks equality only, not which is bigger or smaller whichcopareTo
does and which is required to establish sorting order.
– Pshemo
Nov 16 at 9:41
Do you use accented letters (or umlauts etc)?
– DodgyCodeException
Nov 16 at 9:47
@DodgyCodeException yes I do, sry for forgetting to Mention that. There's the possibility of accented letters existing like for example: ä, ö, ü, è, é, etc...
– D.Mendes
Nov 16 at 9:51
@D.Mendes While accented characters are handled by ignoring the case, it will assume a != ä but ä == Ä
– Peter Lawrey
Nov 16 at 9:53
|
show 1 more comment
1
use.equalsIgnoreCase()
– Sofo Gial
Nov 16 at 9:40
1
@SofoGial That checks equality only, not which is bigger or smaller whichcopareTo
does and which is required to establish sorting order.
– Pshemo
Nov 16 at 9:41
Do you use accented letters (or umlauts etc)?
– DodgyCodeException
Nov 16 at 9:47
@DodgyCodeException yes I do, sry for forgetting to Mention that. There's the possibility of accented letters existing like for example: ä, ö, ü, è, é, etc...
– D.Mendes
Nov 16 at 9:51
@D.Mendes While accented characters are handled by ignoring the case, it will assume a != ä but ä == Ä
– Peter Lawrey
Nov 16 at 9:53
1
1
use
.equalsIgnoreCase()
– Sofo Gial
Nov 16 at 9:40
use
.equalsIgnoreCase()
– Sofo Gial
Nov 16 at 9:40
1
1
@SofoGial That checks equality only, not which is bigger or smaller which
copareTo
does and which is required to establish sorting order.– Pshemo
Nov 16 at 9:41
@SofoGial That checks equality only, not which is bigger or smaller which
copareTo
does and which is required to establish sorting order.– Pshemo
Nov 16 at 9:41
Do you use accented letters (or umlauts etc)?
– DodgyCodeException
Nov 16 at 9:47
Do you use accented letters (or umlauts etc)?
– DodgyCodeException
Nov 16 at 9:47
@DodgyCodeException yes I do, sry for forgetting to Mention that. There's the possibility of accented letters existing like for example: ä, ö, ü, è, é, etc...
– D.Mendes
Nov 16 at 9:51
@DodgyCodeException yes I do, sry for forgetting to Mention that. There's the possibility of accented letters existing like for example: ä, ö, ü, è, é, etc...
– D.Mendes
Nov 16 at 9:51
@D.Mendes While accented characters are handled by ignoring the case, it will assume a != ä but ä == Ä
– Peter Lawrey
Nov 16 at 9:53
@D.Mendes While accented characters are handled by ignoring the case, it will assume a != ä but ä == Ä
– Peter Lawrey
Nov 16 at 9:53
|
show 1 more comment
4 Answers
4
active
oldest
votes
You have two options provided in String itself:
String.compareToIgnoreCase(String)
: case insensitive variant ofcompareTo
)
String.CASE_INSENSITIVE_ORDER
:Comparator
that has the same ordering ascompareToIgnoreCase
- or, for more advanced options like locale-specific rules,
java.text.Collator
andjava.text.RuleBasedCollator
As a tip: your first stop should be the Javadoc, not post a question on Stack Overflow: the Javadoc is extensive and will usually provide a quick answer.
2
Or java.text.Collator.
– DodgyCodeException
Nov 16 at 9:45
Yes, what @DodgyCodeException said, in case locale dependant order is required.
– Pshemo
Nov 16 at 9:47
@DodgyCodeException Thanks, added a link to that one as well.
– Mark Rotteveel
Nov 16 at 9:48
add a comment |
Use String.CASE_INSENSITIVE_ORDER.compare
What about accented letters?
– DodgyCodeException
Nov 16 at 9:42
@DodgyCodeException what about them?
– luk2302
Nov 16 at 9:42
@luk2302 CASE_INSENSITIVE_ORDER doesn't take locales into account. So it might deem f to come before é. The OP didn't specify whether é might possibly be present but it's not up to us to assume it won't be.
– DodgyCodeException
Nov 16 at 9:44
add a comment |
use compareToIgnoreCase() method
https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-
docs.oracle.com/javase/9/docs/api/java/lang/…
– e.g78
Nov 16 at 9:44
add a comment |
compareToIgnoreCase
The String Api has a second compare to function that does a compare while ignoring case.
string1.compareToIgnoreCase(string2);
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have two options provided in String itself:
String.compareToIgnoreCase(String)
: case insensitive variant ofcompareTo
)
String.CASE_INSENSITIVE_ORDER
:Comparator
that has the same ordering ascompareToIgnoreCase
- or, for more advanced options like locale-specific rules,
java.text.Collator
andjava.text.RuleBasedCollator
As a tip: your first stop should be the Javadoc, not post a question on Stack Overflow: the Javadoc is extensive and will usually provide a quick answer.
2
Or java.text.Collator.
– DodgyCodeException
Nov 16 at 9:45
Yes, what @DodgyCodeException said, in case locale dependant order is required.
– Pshemo
Nov 16 at 9:47
@DodgyCodeException Thanks, added a link to that one as well.
– Mark Rotteveel
Nov 16 at 9:48
add a comment |
You have two options provided in String itself:
String.compareToIgnoreCase(String)
: case insensitive variant ofcompareTo
)
String.CASE_INSENSITIVE_ORDER
:Comparator
that has the same ordering ascompareToIgnoreCase
- or, for more advanced options like locale-specific rules,
java.text.Collator
andjava.text.RuleBasedCollator
As a tip: your first stop should be the Javadoc, not post a question on Stack Overflow: the Javadoc is extensive and will usually provide a quick answer.
2
Or java.text.Collator.
– DodgyCodeException
Nov 16 at 9:45
Yes, what @DodgyCodeException said, in case locale dependant order is required.
– Pshemo
Nov 16 at 9:47
@DodgyCodeException Thanks, added a link to that one as well.
– Mark Rotteveel
Nov 16 at 9:48
add a comment |
You have two options provided in String itself:
String.compareToIgnoreCase(String)
: case insensitive variant ofcompareTo
)
String.CASE_INSENSITIVE_ORDER
:Comparator
that has the same ordering ascompareToIgnoreCase
- or, for more advanced options like locale-specific rules,
java.text.Collator
andjava.text.RuleBasedCollator
As a tip: your first stop should be the Javadoc, not post a question on Stack Overflow: the Javadoc is extensive and will usually provide a quick answer.
You have two options provided in String itself:
String.compareToIgnoreCase(String)
: case insensitive variant ofcompareTo
)
String.CASE_INSENSITIVE_ORDER
:Comparator
that has the same ordering ascompareToIgnoreCase
- or, for more advanced options like locale-specific rules,
java.text.Collator
andjava.text.RuleBasedCollator
As a tip: your first stop should be the Javadoc, not post a question on Stack Overflow: the Javadoc is extensive and will usually provide a quick answer.
edited Nov 16 at 9:59
answered Nov 16 at 9:42
Mark Rotteveel
59.2k1476119
59.2k1476119
2
Or java.text.Collator.
– DodgyCodeException
Nov 16 at 9:45
Yes, what @DodgyCodeException said, in case locale dependant order is required.
– Pshemo
Nov 16 at 9:47
@DodgyCodeException Thanks, added a link to that one as well.
– Mark Rotteveel
Nov 16 at 9:48
add a comment |
2
Or java.text.Collator.
– DodgyCodeException
Nov 16 at 9:45
Yes, what @DodgyCodeException said, in case locale dependant order is required.
– Pshemo
Nov 16 at 9:47
@DodgyCodeException Thanks, added a link to that one as well.
– Mark Rotteveel
Nov 16 at 9:48
2
2
Or java.text.Collator.
– DodgyCodeException
Nov 16 at 9:45
Or java.text.Collator.
– DodgyCodeException
Nov 16 at 9:45
Yes, what @DodgyCodeException said, in case locale dependant order is required.
– Pshemo
Nov 16 at 9:47
Yes, what @DodgyCodeException said, in case locale dependant order is required.
– Pshemo
Nov 16 at 9:47
@DodgyCodeException Thanks, added a link to that one as well.
– Mark Rotteveel
Nov 16 at 9:48
@DodgyCodeException Thanks, added a link to that one as well.
– Mark Rotteveel
Nov 16 at 9:48
add a comment |
Use String.CASE_INSENSITIVE_ORDER.compare
What about accented letters?
– DodgyCodeException
Nov 16 at 9:42
@DodgyCodeException what about them?
– luk2302
Nov 16 at 9:42
@luk2302 CASE_INSENSITIVE_ORDER doesn't take locales into account. So it might deem f to come before é. The OP didn't specify whether é might possibly be present but it's not up to us to assume it won't be.
– DodgyCodeException
Nov 16 at 9:44
add a comment |
Use String.CASE_INSENSITIVE_ORDER.compare
What about accented letters?
– DodgyCodeException
Nov 16 at 9:42
@DodgyCodeException what about them?
– luk2302
Nov 16 at 9:42
@luk2302 CASE_INSENSITIVE_ORDER doesn't take locales into account. So it might deem f to come before é. The OP didn't specify whether é might possibly be present but it's not up to us to assume it won't be.
– DodgyCodeException
Nov 16 at 9:44
add a comment |
Use String.CASE_INSENSITIVE_ORDER.compare
Use String.CASE_INSENSITIVE_ORDER.compare
answered Nov 16 at 9:40
talex
9,4201546
9,4201546
What about accented letters?
– DodgyCodeException
Nov 16 at 9:42
@DodgyCodeException what about them?
– luk2302
Nov 16 at 9:42
@luk2302 CASE_INSENSITIVE_ORDER doesn't take locales into account. So it might deem f to come before é. The OP didn't specify whether é might possibly be present but it's not up to us to assume it won't be.
– DodgyCodeException
Nov 16 at 9:44
add a comment |
What about accented letters?
– DodgyCodeException
Nov 16 at 9:42
@DodgyCodeException what about them?
– luk2302
Nov 16 at 9:42
@luk2302 CASE_INSENSITIVE_ORDER doesn't take locales into account. So it might deem f to come before é. The OP didn't specify whether é might possibly be present but it's not up to us to assume it won't be.
– DodgyCodeException
Nov 16 at 9:44
What about accented letters?
– DodgyCodeException
Nov 16 at 9:42
What about accented letters?
– DodgyCodeException
Nov 16 at 9:42
@DodgyCodeException what about them?
– luk2302
Nov 16 at 9:42
@DodgyCodeException what about them?
– luk2302
Nov 16 at 9:42
@luk2302 CASE_INSENSITIVE_ORDER doesn't take locales into account. So it might deem f to come before é. The OP didn't specify whether é might possibly be present but it's not up to us to assume it won't be.
– DodgyCodeException
Nov 16 at 9:44
@luk2302 CASE_INSENSITIVE_ORDER doesn't take locales into account. So it might deem f to come before é. The OP didn't specify whether é might possibly be present but it's not up to us to assume it won't be.
– DodgyCodeException
Nov 16 at 9:44
add a comment |
use compareToIgnoreCase() method
https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-
docs.oracle.com/javase/9/docs/api/java/lang/…
– e.g78
Nov 16 at 9:44
add a comment |
use compareToIgnoreCase() method
https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-
docs.oracle.com/javase/9/docs/api/java/lang/…
– e.g78
Nov 16 at 9:44
add a comment |
use compareToIgnoreCase() method
https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-
use compareToIgnoreCase() method
https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-
answered Nov 16 at 9:42
e.g78
1915
1915
docs.oracle.com/javase/9/docs/api/java/lang/…
– e.g78
Nov 16 at 9:44
add a comment |
docs.oracle.com/javase/9/docs/api/java/lang/…
– e.g78
Nov 16 at 9:44
docs.oracle.com/javase/9/docs/api/java/lang/…
– e.g78
Nov 16 at 9:44
docs.oracle.com/javase/9/docs/api/java/lang/…
– e.g78
Nov 16 at 9:44
add a comment |
compareToIgnoreCase
The String Api has a second compare to function that does a compare while ignoring case.
string1.compareToIgnoreCase(string2);
add a comment |
compareToIgnoreCase
The String Api has a second compare to function that does a compare while ignoring case.
string1.compareToIgnoreCase(string2);
add a comment |
compareToIgnoreCase
The String Api has a second compare to function that does a compare while ignoring case.
string1.compareToIgnoreCase(string2);
compareToIgnoreCase
The String Api has a second compare to function that does a compare while ignoring case.
string1.compareToIgnoreCase(string2);
answered Nov 16 at 9:42
Dinomaster
2366
2366
add a comment |
add a comment |
1
use
.equalsIgnoreCase()
– Sofo Gial
Nov 16 at 9:40
1
@SofoGial That checks equality only, not which is bigger or smaller which
copareTo
does and which is required to establish sorting order.– Pshemo
Nov 16 at 9:41
Do you use accented letters (or umlauts etc)?
– DodgyCodeException
Nov 16 at 9:47
@DodgyCodeException yes I do, sry for forgetting to Mention that. There's the possibility of accented letters existing like for example: ä, ö, ü, è, é, etc...
– D.Mendes
Nov 16 at 9:51
@D.Mendes While accented characters are handled by ignoring the case, it will assume a != ä but ä == Ä
– Peter Lawrey
Nov 16 at 9:53