文本中寻找某一单词出现次数
1 public class Temp { 2 3 public static void main(String[] args) { 4 String s = "frgjavajjavajjavahhjavajavajjjjavahjavahjavajava"; 5 6 // StringBuffer sb = new StringBuffer(); 7 // String[] str = s.split("java"); 8 // for (int i = 0; i < str.length; i++) { 9 // if (str[i] != " ") { 10 // sb.append(str[i]); 11 // } 12 // } 13 // System.out.println((s.length() - sb.length()) / 4); 14 15 int n = 0; 16 int i=0; 17 while (s.length() > 5) { 18 i = s.indexOf("java"); 19 n++; 20 s = s.substring(i + 4); 21 } 22 if (s.contains("java")) { 23 n++; 24 } 25 System.out.println(n); 26 } 27 28 }