在 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

更新日期: 2020 年 2 月 26 日

9K+ 檢視

開啟你的職業生涯

完成課程獲取認證資格

開始
廣告
© . All rights reserved.