/** |
* 查找子字符串 |
* |
* @param str |
*/ |
public static void testFindStr(String str) { |
// indexOf返回子字符串在字符串中的最先出现的位置,如果不存在,返回负数。 |
System.out.println( "str.indexOf(\"is\") = " + str.indexOf( "is" )); |
// 可以给indexOf方法设置参数,指定匹配的起始位置 |
System.out.println( "str.indexOf(\"is\", 4) = " + str.indexOf( "is" , 4 )); |
// lastIndexOf返回子字符串在字符串中的最后出现的位置,如果不存在,返回负数。 |
System.out |
.println( "str.lastIndexOf(\"is\") = " + str.lastIndexOf( "is" )); |
// 可以给lastIndexOf方法设置参数,指定匹配的结束位置 |
System.out.println( "str.lastIndexOf(\"is\", 1) = " |
+ str.lastIndexOf( "is" , 1 )); |
} |