Search code examples
javacase-sensitive

indexOf Case Sensitive?


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


Solution

  • The indexOf() methods are all case-sensitive. You can make them (roughly, in a broken way, but working for plenty of cases) case-insensitive by converting your strings to upper/lower case beforehand:

    s1 = s1.toLowerCase(Locale.US);
    s2 = s2.toLowerCase(Locale.US);
    s1.indexOf(s2);