Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

something like equalsIgnoreCase while using indexOf

I am using this code, to get the index of a String in an Array.

int n = Arrays.asList(Names).indexOf(textBox.getText());

The problem here is, if the String in textBox is different in case to its similar String in the Array. It returns -1. How can make it something like equalsIgnoreCase in case of String comparision.

Thank You

like image 273
Archie.bpgc Avatar asked Dec 11 '12 09:12

Archie.bpgc


1 Answers

You can use the Collator class. in Here you can set different levels for your comparison. you can ignore lower and upper cases, and set some specific language charackters. In German for example it can set ß equal to ss.

here´s some documentary: Collator class

Edit : here´s an example Code for you

private int indexOf(String[] original, String search) {
    Collator collator = Collator.getInstance(); 
    collator.setStrength(Collator.SECONDARY);
    for(int i = 0;i<original.length;++i) {
        if(collator.equals(search, original[i]))
            return i;
    }
    return -1;
}
like image 98
SomeJavaGuy Avatar answered Oct 06 '22 00:10

SomeJavaGuy