在Java中搜索字符串中的字符。
本文向大家介绍在Java中搜索字符串中的字符。,包括了在Java中搜索字符串中的字符。的使用技巧和注意事项,需要的朋友参考一下
您可以使用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