溫馨提示×

java中indexof的作用是什么

小億
115
2024-02-20 09:56:29
欄目: 編程語言

在Java中,indexOf()方法用于返回指定字符串在字符串中第一次出現(xiàn)的位置索引。如果找不到指定字符串,則返回-1。該方法的語法為:

int indexOf(String str)

其中,str為要查找的字符串。

例如:

String str = "Hello World";
int index = str.indexOf("World");
System.out.println(index); // 輸出為 6

上面的例子中,indexOf()方法返回了"World"在字符串"Hello World"中的位置索引,即6。

0