Is there a way to compare 2 Strings without it being case-sensitive? [duplicate]












5















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 Comparatorand 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.










share|improve this question















marked as duplicate by hellow, chŝdk, SpaceTrucker, Stephen C java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 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
















5















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 Comparatorand 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.










share|improve this question















marked as duplicate by hellow, chŝdk, SpaceTrucker, Stephen C java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 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














5












5








5








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 Comparatorand 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.










share|improve this question
















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 Comparatorand 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 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














  • 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








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












4 Answers
4






active

oldest

votes


















12














You have two options provided in String itself:





  • String.compareToIgnoreCase(String): case insensitive variant of compareTo)


  • String.CASE_INSENSITIVE_ORDER: Comparator that has the same ordering as compareToIgnoreCase

  • or, for more advanced options like locale-specific rules, java.text.Collator and java.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.






share|improve this 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



















6














Use String.CASE_INSENSITIVE_ORDER.compare






share|improve this answer





















  • 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



















1














use compareToIgnoreCase() method



https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-






share|improve this answer





















  • docs.oracle.com/javase/9/docs/api/java/lang/…
    – e.g78
    Nov 16 at 9:44



















0














compareToIgnoreCase



The String Api has a second compare to function that does a compare while ignoring case.



string1.compareToIgnoreCase(string2);





share|improve this answer




























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    You have two options provided in String itself:





    • String.compareToIgnoreCase(String): case insensitive variant of compareTo)


    • String.CASE_INSENSITIVE_ORDER: Comparator that has the same ordering as compareToIgnoreCase

    • or, for more advanced options like locale-specific rules, java.text.Collator and java.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.






    share|improve this 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
















    12














    You have two options provided in String itself:





    • String.compareToIgnoreCase(String): case insensitive variant of compareTo)


    • String.CASE_INSENSITIVE_ORDER: Comparator that has the same ordering as compareToIgnoreCase

    • or, for more advanced options like locale-specific rules, java.text.Collator and java.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.






    share|improve this 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














    12












    12








    12






    You have two options provided in String itself:





    • String.compareToIgnoreCase(String): case insensitive variant of compareTo)


    • String.CASE_INSENSITIVE_ORDER: Comparator that has the same ordering as compareToIgnoreCase

    • or, for more advanced options like locale-specific rules, java.text.Collator and java.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.






    share|improve this answer














    You have two options provided in String itself:





    • String.compareToIgnoreCase(String): case insensitive variant of compareTo)


    • String.CASE_INSENSITIVE_ORDER: Comparator that has the same ordering as compareToIgnoreCase

    • or, for more advanced options like locale-specific rules, java.text.Collator and java.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.







    share|improve this answer














    share|improve this answer



    share|improve this 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














    • 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













    6














    Use String.CASE_INSENSITIVE_ORDER.compare






    share|improve this answer





















    • 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
















    6














    Use String.CASE_INSENSITIVE_ORDER.compare






    share|improve this answer





















    • 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














    6












    6








    6






    Use String.CASE_INSENSITIVE_ORDER.compare






    share|improve this answer












    Use String.CASE_INSENSITIVE_ORDER.compare







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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











    1














    use compareToIgnoreCase() method



    https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-






    share|improve this answer





















    • docs.oracle.com/javase/9/docs/api/java/lang/…
      – e.g78
      Nov 16 at 9:44
















    1














    use compareToIgnoreCase() method



    https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-






    share|improve this answer





















    • docs.oracle.com/javase/9/docs/api/java/lang/…
      – e.g78
      Nov 16 at 9:44














    1












    1








    1






    use compareToIgnoreCase() method



    https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-






    share|improve this answer












    use compareToIgnoreCase() method



    https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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











    0














    compareToIgnoreCase



    The String Api has a second compare to function that does a compare while ignoring case.



    string1.compareToIgnoreCase(string2);





    share|improve this answer


























      0














      compareToIgnoreCase



      The String Api has a second compare to function that does a compare while ignoring case.



      string1.compareToIgnoreCase(string2);





      share|improve this answer
























        0












        0








        0






        compareToIgnoreCase



        The String Api has a second compare to function that does a compare while ignoring case.



        string1.compareToIgnoreCase(string2);





        share|improve this answer












        compareToIgnoreCase



        The String Api has a second compare to function that does a compare while ignoring case.



        string1.compareToIgnoreCase(string2);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 at 9:42









        Dinomaster

        2366




        2366















            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?