Java中 獲取指定字符串在另一個字符串中出現的次數,供大家參考,具體內容如下
/** * @param args */ public static void main(String[] args) { String srcText = "Hello World"; String findText = "e"; int num = appearNumber(srcText, findText); System.out.println(num); } /** * 獲取指定字符串出現的次數 * * @param srcText 源字符串 * @param findText 要查找的字符串 * @return */ public static int appearNumber(String srcText, String findText) { int count = 0; Pattern p = Pattern.compile(findText); Matcher m = p.matcher(srcText); while (m.find()) { count++; } return count; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選