扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

java中indexof的作用是什么

扬州沐宇科技
2024-02-20 09:56:29
Java

在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。

扫码添加客服微信