indexOf Case Sensitive?

后端 未结 19 1128
日久生厌
日久生厌 2020-11-27 19:39

Is the indexOf(String) method case sensitive? If so, is there a case insensitive version of it?

相关标签:
19条回答
  • 2020-11-27 19:50

    The first question has already been answered many times. Yes, the String.indexOf() methods are all case-sensitive.

    If you need a locale-sensitive indexOf() you could use the Collator. Depending on the strength value you set you can get case insensitive comparison, and also treat accented letters as the same as the non-accented ones, etc. Here is an example of how to do this:

    private int indexOf(String original, String search) {
        Collator collator = Collator.getInstance();
        collator.setStrength(Collator.PRIMARY);
        for (int i = 0; i <= original.length() - search.length(); i++) {
            if (collator.equals(search, original.substring(i, i + search.length()))) {
                return i;
            }
        }
        return -1;
    }
    
    0 讨论(0)
  • 2020-11-27 19:53

    But it's not hard to write one:

    public class CaseInsensitiveIndexOfTest extends TestCase {
        public void testOne() throws Exception {
            assertEquals(2, caseInsensitiveIndexOf("ABC", "xxabcdef"));
        }
    
        public static int caseInsensitiveIndexOf(String substring, String string) {
            return string.toLowerCase().indexOf(substring.toLowerCase());
        }
    }
    
    0 讨论(0)
  • 2020-11-27 19:54

    Just to sum it up, 3 solutions:

    • using toLowerCase() or toUpperCase
    • using StringUtils of apache
    • using regex

    Now, what I was wondering was which one is the fastest? I'm guessing on average the first one.

    0 讨论(0)
  • 2020-11-27 19:55

    Yes, I am fairly sure it is. One method of working around that using the standard library would be:

    int index = str.toUpperCase().indexOf("FOO"); 
    
    0 讨论(0)
  • 2020-11-27 19:58

    Yes, it is case-sensitive. You can do a case-insensitive indexOf by converting your String and the String parameter both to upper-case before searching.

    String str = "Hello world";
    String search = "hello";
    str.toUpperCase().indexOf(search.toUpperCase());
    

    Note that toUpperCase may not work in some circumstances. For instance this:

    String str = "Feldbergstraße 23, Mainz";
    String find = "mainz";
    int idxU = str.toUpperCase().indexOf (find.toUpperCase ());
    int idxL = str.toLowerCase().indexOf (find.toLowerCase ());
    

    idxU will be 20, which is wrong! idxL will be 19, which is correct. What's causing the problem is tha toUpperCase() converts the "ß" character into TWO characters, "SS" and this throws the index off.

    Consequently, always stick with toLowerCase()

    0 讨论(0)
  • Had the same problem. I tried regular expression and the apache StringUtils.indexOfIgnoreCase-Method, but both were pretty slow... So I wrote an short method myself...:

    public static int indexOfIgnoreCase(final String chkstr, final String searchStr, int i) {
        if (chkstr != null && searchStr != null && i > -1) {
              int serchStrLength = searchStr.length();
              char[] searchCharLc = new char[serchStrLength];
              char[] searchCharUc = new char[serchStrLength];
              searchStr.toUpperCase().getChars(0, serchStrLength, searchCharUc, 0);
              searchStr.toLowerCase().getChars(0, serchStrLength, searchCharLc, 0);
              int j = 0;
              for (int checkStrLength = chkstr.length(); i < checkStrLength; i++) {
                    char charAt = chkstr.charAt(i);
                    if (charAt == searchCharLc[j] || charAt == searchCharUc[j]) {
                         if (++j == serchStrLength) {
                               return i - j + 1;
                         }
                    } else { // faster than: else if (j != 0) {
                             i = i - j;
                             j = 0;
                        }
                  }
            }
            return -1;
      }
    

    According to my tests its much faster... (at least if your searchString is rather short). if you have any suggestions for improvement or bugs it would be nice to let me know... (since I use this code in an application ;-)

    0 讨论(0)
提交回复
热议问题