在 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