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