public class TestSearchIndex {
public static void main(String[] args) {
String one = "小笼包子";
String two = "包";
System.out.println("方法1:" + searchIndex(one, two)); //方法1:2
System.out.println("方法2:" + searchIndex2(one, two)); //方法2:2
}
//方法1
static int searchIndex(String one, String two) {
Matcher matcher = Pattern.compile(two).matcher(one);
/* if (matcher.find()) {
return matcher.start();
} else {
return -1;
}
*/
return matcher.find()?matcher.start():-1;
}
//方法2
static int searchIndex2(String one, String two) {
return one.indexOf(two);
}
}