The following example uses the indexOf() method of the String class to find the position where the substring appears in the string. If it exists, it returns the position where the string appears (the first bit is 0). If it does not exist, it returns -1:
//SearchStringEmp.java 文件public class SearchStringEmp{ public static void main(String[] args) { String strOrig = "Hello readers"; int intIndex = strOrig.indexOf("Hello"); if(intIndex == - 1){ System.out.println("Hello not found"); }else{ System.out.println("Found Hello at index " + intIndex); } }}
The output result of the above code example is:
Found Hello at index 0
The above is the content of Java example-string search. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!