在 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