java中indexof方法如何调用

2024-04-30

在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’ 在字符串中第一次出现的位置,并将结果打印出来。

《java中indexof方法如何调用.doc》

下载本文的Word格式文档,以方便收藏与打印。