Java中 獲取指定字符串在另一個字符串中出現(xiàn)的次數(shù),供大家參考,具體內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
/** * @param args */ public static void main(String[] args) { String srcText = "Hello World" ; String findText = "e" ; int num = appearNumber(srcText, findText); System.out.println(num); } /** * 獲取指定字符串出現(xiàn)的次數(shù) * * @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; } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/libing06081227/article/details/51991990