Java String indexOf()方法有什么作用呢?
Java String indexOf()方法的功能
IndexOf()方法的功能:用于返回指定字符在字符串中第一次出现的索引
如果未出现指定字符,则返回-1
indexOf()语法
public int indexOf(char ch)
参数说明
ch
:待检索第一次出现索引位置的字符
返回值
- 返回
ch
所在的索引值
例
import java.io.*;
public class testClass {
public static void main(String args[]) {
String Str = new String("Welcome to java265.com");
System.out.print("index:" );
System.out.println(Str.indexOf( 'j' ));
}
}
-----运行以上代码,将输出以下信息----
index :11
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。