方法一
public int count(String srcText, String findText) { String[] array = srcText.split(findText); return array.length - 1; }
方法二
public static int count(String srcText, String findText) { int count = 0; Pattern p = Pattern.compile(findText); Matcher m = p.matcher(srcText); while (m.find()) { count++; } return count; }
方法三
public static int count(String srcText, String findText) { int count = 0; int index = 0; while ((index = srcText.indexOf(findText, index)) != -1) { index = index + findText.length(); count++; } return count; }
时间: 2024-10-26 19:09:42