提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用indexOf搜索字符串
public class Main { /*时 代 J a v a 公 众 号 - nowjava.com 提供*/ public static void main(String[] args) { //declare a String object String strOrig = "Hello world Hello World"; int intIndex = strOrig.indexOf("Hello"); if(intIndex == - 1){ System.out.println("Hello not found"); }else{ System.out.println("Found Hello at index " + intIndex); } int positionIndex = strOrig.indexOf("Hello",11); System.out.println("Index of Hello after 11 is " + positionIndex); //from 时 代 J a v a int