如何在 java 中搜索字符或子字符串?

java 8object oriented programmingprogramming

您可以使用 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

相关文章