Java indexOf 区分大小写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1126227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:03:08  来源:igfitidea点击:

indexOf Case Sensitive?

javacase-sensitive

提问by Brian

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

indexOf(String) 方法是否区分大小写?如果是这样,是否有不区分大小写的版本?

采纳答案by Joey

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:

这些indexOf()方法都是区分大小写的。您可以通过预先将字符串转换为大写/小写来使它们(粗略地,以一种破碎的方式,但适用于很多情况)不区分大小写:

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

回答by jjnguy

Yes, indexOfis case sensitive.

是的,indexOf区分大小写。

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

这将不区分大小写indexOf()

回答by dfa

Is the indexOf(String) method case sensitive?

indexOf(String) 方法是否区分大小写?

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:

不,没有。您可以在调用 indexOf 之前将两个字符串转换为小写:

@Test
public void caseInsensitiveIndexOf() {
    assertTrue("Hello World!".toLowerCase().indexOf("Hello".toLowerCase()) != -1);
    assertTrue("Hello World!".toLowerCase().indexOf("hello".toLowerCase()) != -1);
}

回答by Paul McKenzie

@Test
public void testIndexofCaseSensitive() {
    TestCase.assertEquals(-1, "abcDef".indexOf("d") );
}

回答by Yacoby

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"); 

回答by Nick Lewis

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

是的,它区分大小写。您可以indexOf通过在搜索之前将 String 和 String 参数都转换为大写来实现不区分大小写。

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

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

请注意, toUpperCase 在某些情况下可能不起作用。例如这个:

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.

idxU 将是 20,这是错误的!idxL 将是 19,这是正确的。导致问题的原因是 toUpperCase() 转换了“?” 字符转换为两个字符,“SS”,这会导致索引关闭。

Consequently, always stick with toLowerCase()

因此,始终坚持使用 toLowerCase()

回答by John Topley

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

我刚刚看了源码。它比较字符,因此区分大小写。

回答by Robbie

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.

indexOf 区分大小写。这是因为它使用 equals 方法比较列表中的元素。包含和删除也是如此。

回答by Carl Manaster

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());
    }
}

回答by toolkit

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);
    }
}