1. //统计某个字符出现的次数
  2. private void countSubString(){
  3. String string1="香蕉、玉米、面粉";
  4. String string2="香蕉、玉米、面粉";
  5. String string3="牛奶、鸡蛋";
  6. StringBuffer stringBuffer=new StringBuffer();
  7. stringBuffer.append(string1).append("、").append(string2).append("、").append(string3).append("、");
  8. String totalString=stringBuffer.toString();
  9. System.out.println("组拼后的字符串为:"+totalString);
  10. while (totalString.length()>0) {
  11. //得到第一个字符串比如"香蕉、"
  12. int index=totalString.indexOf("、");
  13. String foodName=totalString.substring(0,index+1);
  14. Pattern pattern = Pattern.compile(foodName);
  15. Matcher matcher = pattern.matcher(totalString);
  16. int count=0;
  17. while(matcher.find()){
  18. count++;
  19. }
  20. totalString= totalString.replaceAll(foodName, "");
  21. System.out.println("食品名字为:"+foodName+",出现次数为:"+count);
  22. System.out.println("统计删除后字符串为:totalString="+totalString);
  23. System.out.println("===============================");
  24. }
  25. }

Java利用正则表达式统计某个字符串出现的次数的更多相关文章

  1. Java利用递归算法统计1-6的数组排列组合数

    Java利用递归算法统计1-6的数组排列组合数 1.设计源码 /** * @Title:ArrayCombination.java * @Package:com.you.data * @Descrip ...

  2. java怎么实现统计一个字符串中字符出现的次数

    问题:假设字符串仅仅保护a-z 的字母,java怎么实现统计一个字符串中字符出现的次数?而且,如果压缩后的字符数不小于原始字符数,则返回. 处理逻辑:首先拆分字符串,以拆分出的字符为key,以字符出现 ...

  3. Linux 统计某个字符串出现的次数

    要统计一个字符串出现的次数,这里现提供自己常用两种方法: 1. 使用vim统计 用vim打开目标文件,在命令模式下,输入 :%s/objStr//gn 即可 2. 使用grep: grep -o ob ...

  4. 我的Java开发学习之旅------>工具类:Java使用正则表达式分离出字符串中的中文和英文

    今天看到一个工具类使用正则表达式将一大段字符串中的中文和英文都分离出来了,在此记录一下,读者可以收藏! import java.util.ArrayList; import java.util.Col ...

  5. 在Java中用正则表达式判断一个字符串是否是数字的方法

    package chengyujia; import java.util.regex.Pattern; public class NumberUtil { /** * 判断一个字符串是否是数字. * ...

  6. Java 利用Gson将json字符串转换为List<Map<String, String>>

    json字符串类似于: [ { "userPhone": "123", "userNo": "123-2", " ...

  7. java中用正则表达式判断中文字符串中是否含有英文或者数字

    public static boolean includingNUM(String str)throws  Exception{ Pattern p  = Pattern.compile(" ...

  8. java利用SuffixFileFilter统计目录下特定后缀名文件的数目

    /** * 文件处理类 * @author zhangcd * @date 2017年1月3日 */ public class FileUtil { /** * 得到所有后缀的数目 * * @para ...

  9. 给定字符串数组,用map的key保存数组中字符串元素,value保存字符串元素出现次数,最后统计个字符串元素出现次数

    import java.util.HashMap; public class map1 { public static void main(String[] args) { String[] arra ...

随机推荐

  1. SQLServer数据类型与C#类型对照表

    这是我在开发多层体系结构时遇到的问题,由于VS.NET尚不支持各种数据类型的空值即null, 放入实体类中在各个层之间作为参数传递,所以经查询参考SQL Server 2000的联机丛书和.NET的M ...

  2. 如何让Activiti-Explorer使用sql server数据库

    从官网下载的Activiti-explorer的war文件内部默认是使用h2内存数据库的,如果想改用其他的数据库来做持久化,比如sql server,需要做如下配置. 1)修改db.propertie ...

  3. Memcached总结二:Memcached环境安装设置以及连接memcache服务器

    1 在Ubuntu上安装Memcached 要在Ubuntu上安装Memcached,打开终端,然后输入以下命令: $sudo apt-get update $sudo apt-get install ...

  4. mac 浏览器 强刷快捷键

    windows 浏览器的刷新快捷键F5,强制刷新Ctrl+F5 Mac 系统下浏览器的刷新快捷键 command+R, 强制刷新快捷键为 command+shift+R

  5. Where is the ActiveX Project Type for Delphi 10.1 Berlin

    n 10.1 Berlin the ActiveX project types are missing from the New Items Window under Delphi. They are ...

  6. 139. Word Break

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  7. java路径中的空格问题(转)

    java路径中的空格问题 1. URLTest.class.getResource("/").getPath(); URLTest.class.getResource(" ...

  8. 坑爹的libxml2 for mingw 编译

    按照官方的readerme.txt说法生成Makefile之后,你会发现编译时候需要创建几个文件夹. 还有就是因为宏定义问题,报错,需要在config.h中加入#define HAVE_STDINT_ ...

  9. bzoj1296

    首先先预处理每行刷1~m次最多能正确涂出多少格 然后把每行涂色看做一个物品,当重量为j(这行涂了j次),价值为对应能正确涂出的格子数: 总重量为k,然后做分组背包即可 ..,..,..] of lon ...

  10. Eclipse 中使用Genymotion 作为模拟器的步骤

    我这里是先安装的genymotion, 后安装的eclipse. 1:安装genymotion 无难度, 直接安装就行了. 2:安装eclipse 下载adt即可, 解压运行. 3:运行eclipse ...