-
Java利用正则表达式统计某个字符串出现的次数
-
- private void countSubString(){
- String string1="香蕉、玉米、面粉";
- String string2="香蕉、玉米、面粉";
- String string3="牛奶、鸡蛋";
- StringBuffer stringBuffer=new StringBuffer();
- stringBuffer.append(string1).append("、").append(string2).append("、").append(string3).append("、");
- String totalString=stringBuffer.toString();
- System.out.println("组拼后的字符串为:"+totalString);
-
- while (totalString.length()>0) {
-
- int index=totalString.indexOf("、");
- String foodName=totalString.substring(0,index+1);
-
- Pattern pattern = Pattern.compile(foodName);
- Matcher matcher = pattern.matcher(totalString);
- int count=0;
- while(matcher.find()){
- count++;
- }
- totalString= totalString.replaceAll(foodName, "");
- System.out.println("食品名字为:"+foodName+",出现次数为:"+count);
- System.out.println("统计删除后字符串为:totalString="+totalString);
- System.out.println("===============================");
- }
- }
-
相关阅读:
[转]用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
xp_cmdshell开启与关闭
[转]触发器
上午绿茶下午菊花茶晚上枸杞
SQL中CONVERT转化日期函数的使用方法
如何使用url实现数据交互
struts2之form标签theme属性详解
[保险核心] 保险公司集中收付费系统
数据库分页大全
如何使用spring中hibernate返回获取list集合
-
原文地址:https://www.cnblogs.com/zhwl/p/3632133.html
Copyright © 2020-2023
润新知