indexOf Case Sensitive?
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);
There is an ignore case method in StringUtils class of Apache Commons Lang library
indexOfIgnoreCase(CharSequence str, CharSequence searchStr)
Yes, indexOf
is case sensitive.
The best way to do case insensivity I have found is:
String original;
int idx = original.toLowerCase().indexOf(someStr.toLowerCase());
That will do a case insensitive indexOf()
.
Is the indexOf(String) method case sensitive?
Yes, it is case sensitive:
@Test
public void indexOfIsCaseSensitive() {
assertTrue("Hello World!".indexOf("Hello") != -1);
assertTrue("Hello World!".indexOf("hello") == -1);
}
If so, is there a case insensitive version of it?
No, there isn't. You can convert both strings to lower case before calling indexOf:
@Test
public void caseInsensitiveIndexOf() {
assertTrue("Hello World!".toLowerCase().indexOf("Hello".toLowerCase()) != -1);
assertTrue("Hello World!".toLowerCase().indexOf("hello".toLowerCase()) != -1);
}