国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java統計字符串中指定元素出現次數方法

2019-11-26 14:45:01
字體:
來源:轉載
供稿:網友

本文實例講解了統計文本中某個字符串出現的次數或字符串中指定元素出現的次數方法,分享給大家供大家參考,具體內容如下

運行效果圖:

程序查找的上此文件帶"a"的字符在多少次

具體代碼如下

package com.zuidaima.util.string; import java.io.*; public class CountString {    public static int count(String filename, String target)   throws FileNotFoundException, IOException {   FileReader fr = new FileReader(filename);   BufferedReader br = new BufferedReader(fr);   StringBuilder strb = new StringBuilder();   while (true) {   String line = br.readLine();   if (line == null) {    break;   }   strb.append(line);   }   String result = strb.toString();   int count = 0;   int index = 0;   while (true) {   index = result.indexOf(target, index + 1);   if (index > 0) {    count++;   } else {    break;   }   }   br.close();   return count;  }    public static void main(String[] args) {   try {   System.out.println(count("D://zuidaima.txt", "a"));   } catch (FileNotFoundException e) {   e.printStackTrace();   } catch (IOException e) {   e.printStackTrace();   }  }   }

  以上就是java統計字符串中指定元素出現次數方法,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 峨山| 织金县| 长治市| 临安市| 龙门县| 延边| 安多县| 佛山市| 宜宾市| 巩义市| 获嘉县| 辛集市| 吴川市| 台北县| 西盟| 天津市| 登封市| 清新县| 五家渠市| 嘉兴市| 财经| 威宁| 蕲春县| 徐州市| 永宁县| 漳浦县| 舒兰市| 屏东市| 孝昌县| 尼玛县| 甘德县| 曲水县| 上蔡县| 防城港市| 明溪县| 鄂托克前旗| 黎城县| 靖安县| 固镇县| 沅陵县| 武冈市|