views:

1372

answers:

10

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

+2  A: 

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().

jjnguy
why downvote? the answer is correct and there is also an example..
dfa
Ha, thanks. I was wondering that myself...I haven't even downvoted the answers I was complaining about...
jjnguy
+10  A: 

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);
}
dfa
Probably the best answer of the group here....
jjnguy
oh please please please don't forget to use culture invariant conversion with Locale.US, we had enough problems with java applications running under Turkish locale.
idursun
sure! it was only a sample code it is not meant for production...
dfa
Yup, please never copy paste my code into prod code...
jjnguy
+6  A: 
@Test
public void testIndexofCaseSensitive() {
    TestCase.assertEquals(-1, "abcDef".indexOf("d") );
}
Paul McKenzie
+1 for answer-by-test.
Carl Manaster
This doesn't even answer the full question..it doesn't even say if the test passes....
jjnguy
You're right I didn't, I was kinda hoping that it would prompt the original questioner to run the test him/herself, and maybe get into the habit
Paul McKenzie
Well, that is fine...but I would argue that it would be better to vote for a question that actually gives an answer than a test. StackOverflow is trying to be a code Q and A repository. Thus full answers would be best.
jjnguy
@jjnguy: I was always under the impression that people who posted tests, posted tests that pass. @dfa kind of did a similar thing. (But @dfa's answer is more complete).
Tom
But he also posted some words(description)...Those are usually helpful.
jjnguy
+6  A: 

The indexOf() methods are all case-sensitive. You can make them case-insensitive by converting your strings to upper/lower case beforehand:

s1 = s1.toLowerCase(Locale.US);
s2 = s2.toLowerCase(Locale.US);
s1.indexOf(s2);
Joey
Before voting for this one, check out some of the other (better answers farther down) dfa's is pretty good.
jjnguy
.toLowerCase(Locale.US)
idursun
Beware of internationalization issues (i.e. the Turkish İ) when using toUpperCase. A more proper solution is to use str.toUpperCase(Locale.US).indexOf(...);
James Van Huis
Jeez, jjnguy. A good answer is a good answer. You don't have to ignore terse, quick answers in favor for detailed ones just by the virtue that the latter answers exist.
Stuart Branham
I will agree that you don't have to ignore them. However, you probably should.
jjnguy
I'm quite sure that case-converting and then comparing is not entirely correct according to Unicode comparison rules. It works for some things (namely case folding, which is generally used only in syntax parsing contexts) but for natural language there can be special cases where two strings that should compare equal don't, under either both uppercase or both lowercase. I can't come up with any examples off the bat however.
nielsm
+1  A: 

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");
Yacoby
+1  A: 

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());
Nick Lewis
+1  A: 

I've just looked at the source. It compares chars so it is case sensitive.

John Topley
A: 

indexOf is case sensitive. This is because it uses the equals method to compare the elements in the list. The same thing goes for contains and remove.

Robbie
The original question is about String's indexOf method.
John Topley
I didn't know that's what he was talking about. I didn't realize it until other people had said something. The principle is still the same though.
Robbie
No it isn't. The internals of String's indexOf method compares chars not objects, so it doesn't use the equals method.
John Topley
+1  A: 

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());
    }
}
Carl Manaster
+1  A: 

What are you doing with the index value once returned?

If you are using it to manipulate your string, then could you not use a regular expression instead?

import static org.junit.Assert.assertEquals;    
import org.junit.Test;

public class StringIndexOfRegexpTest {

    @Test
    public void testNastyIndexOfBasedReplace() {
        final String source = "Hello World";
        final int index = source.toLowerCase().indexOf("hello".toLowerCase());
        final String target = "Hi".concat(source.substring(index
                + "hello".length(), source.length()));
        assertEquals("Hi World", target);
    }

    @Test
    public void testSimpleRegexpBasedReplace() {
        final String source = "Hello World";
        final String target = source.replaceFirst("(?i)hello", "Hi");
        assertEquals("Hi World", target);
    }
}
toolkit