温馨提示×

java中indexof的作用是什么

小亿
90
2024-02-20 09:56:29
栏目: 编程语言

在Java中,indexOf()方法用于返回指定字符串在字符串中第一次出现的位置索引。如果找不到指定字符串,则返回-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