如何在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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程语言
C++
C#
MongoDB
MySQL
JavaScript
PHP