如何在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

更新日期:2019-07-30

169 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.