在Java中搜索字符串中的字符。

2023年12月22日12:56:25

您可以使用String类的indexOf()方法在字符串中搜索特定字母。此方法返回字符串中单词的位置索引(如果找到)。否则返回-1。

示例

public class Test {
   public static void main(String args[]) {
      String str = new String("hi welcome to Nhooo");
      int index = str.indexOf('w');
      System.out.println("Index of the letter w :: "+index);
   }
}

输出结果

Index of the letter w :: 3

  • 更新时间:2023年12月22日12:56:25 ,共 313 字。