在 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

更新時間: 26-Feb-2020

9K+ 閱讀次數

開啟 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.