Java

Java的indexof函数查找范围在哪

小樊
81
2024-09-30 00:13:15
栏目: 编程语言

在Java中,indexOf函数的查找范围是整个字符串。如果字符串中有多个子字符串与指定的字符串匹配,indexOf将返回第一个匹配项的索引。如果没有找到匹配项,则返回-1。

以下是一个简单的示例:

public class Main {
    public static void main(String[] args) {
        String str = "Hello, world!";
        int index = str.indexOf("world");
        System.out.println("Index of 'world': " + index);  // 输出:Index of 'world': 7
    }
}

在这个例子中,indexOf函数在字符串"Hello, world!"中查找子字符串"world",并返回其索引7。

0
看了该问题的人还看了