在 Java 中搜索字符串中的字符。
你可以使用 String 类中的 indexOf() 方法在字符串中搜索某个特定字母。如果找到,此方法将返回一个字符串中单词的位置索引。否则,它将返回 -1。
示例
public class Test {
public static void main(String args[]) {
String str = new String("hi welcome to Tutorialspoint");
int index = str.indexOf('w');
System.out.println("Index of the letter w :: "+index);
}
}输出
Index of the letter w :: 3
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP