public static void main(String[] args){ int num = 0; String str = "sunjavaIBMjavaOracleJavaHellojavaGoodJava"; Pattern pt = Pattern.compile("java", Pattern.CASE_INSENSITIVE); Matcher mc = pt.matcher(str); mc.reset(); while (mc.find()) { num++; String gc = mc.group(); System.out.println("第"+num+"次查找出来的字符串是:" + gc); } System.out.println("java的总数是:" + num); mc.reset(); // 把奇数次找到的 java 转化为大写。偶数次找到的 java 转化为小写 while (mc.find()) { num++; if (num % 2 == 1) { String gc = mc.group(); String o1 = gc.toUpperCase(); System.out.println("转换后的字符串是:"+o1); } else { String gc = mc.group(); String o2 = gc.toLowerCase(); System.out.println("转换后的字符串是:"+o2); } } }