• Java利用正则表达式统计某个字符串出现的次数


    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.           
    11.         while (totalString.length()>0) {  
    12.             //得到第一个字符串比如"香蕉、"  
    13.             int index=totalString.indexOf("、");  
    14.             String foodName=totalString.substring(0,index+1);  
    15.               
    16.             Pattern pattern = Pattern.compile(foodName);    
    17.             Matcher matcher = pattern.matcher(totalString);    
    18.             int count=0;  
    19.             while(matcher.find()){  
    20.                 count++;  
    21.             }  
    22.             totalString= totalString.replaceAll(foodName, "");  
    23.             System.out.println("食品名字为:"+foodName+",出现次数为:"+count);  
    24.             System.out.println("统计删除后字符串为:totalString="+totalString);  
    25.             System.out.println("===============================");  
    26.         }  
    27.     } 
  • 相关阅读:
    [转]用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
    xp_cmdshell开启与关闭
    [转]触发器
    上午绿茶下午菊花茶晚上枸杞
    SQL中CONVERT转化日期函数的使用方法
    如何使用url实现数据交互
    struts2之form标签theme属性详解
    [保险核心] 保险公司集中收付费系统
    数据库分页大全
    如何使用spring中hibernate返回获取list集合
  • 原文地址:https://www.cnblogs.com/zhwl/p/3632133.html
Copyright © 2020-2023  润新知