溫馨提示×

Java的indexof函數(shù)查找范圍在哪

小樊
81
2024-09-30 00:13:15
欄目: 編程語言

在Java中,indexOf函數(shù)的查找范圍是整個字符串。如果字符串中有多個子字符串與指定的字符串匹配,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函數(shù)在字符串"Hello, world!"中查找子字符串"world",并返回其索引7。

0