Java Examples - String Search

Java 实例Java Examples

The following example uses the indexOf() method of the String class to find the position of a substring in a string. If it exists, it returns the position of the substring (the first position is 0); if it does not exist, it returns -1:

SearchStringEmp.java file

public class SearchStringEmp { public static void main(String[] args) { String strOrig = "Google Example Taobao"; int intIndex = strOrig.indexOf("Example"); if(intIndex == - 1){ System.out.println("String Example not found"); }else{ System.out.println("Example string position" + intIndex); } } }

The output of the above code example is:

Example 字符串位置 7

Java 实例Java Examples

Other extensions