java中indexof方法如何调用

avatar
作者
猴君
阅读量:0

在Java中,indexOf方法通常是用来查找指定元素在字符串中第一次出现的位置的。要调用indexOf方法,需要在字符串对象上调用该方法,并传入要查找的元素作为参数。

示例代码如下:

public class Main {     public static void main(String[] args) {         String str = "Hello World";                  int index = str.indexOf("o");         System.out.println("The index of 'o' in the string is: " + index);     } } 

在上面的示例中,我们定义了一个字符串 “Hello World”,然后调用了indexOf方法来查找字符 ‘o’ 在字符串中第一次出现的位置,并将结果打印出来。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!