牛骨文教育服务平台(让学习变的简单)
博文笔记

Java 中计算字符串中子串出现的次数

创建时间:2014-06-09 投稿人: 浏览次数:12377

统计一个字符串中子串出现的次数有以下两种:

1.使用正则表达式处理;

Pattern p = Pattern.compile("abc",Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(str);
        int count = 0;
        while(m.find()){
              count ++;
        }

2.使用普通方法处理,String的split的方法(推荐)

String parent = "select * from t where t.id is null";
		String child = "e";
		
		String[] arr = (","+parent.toLowerCase()+",").split(child);
		System.out.println(arr.length - 1);

对于子字符串的大小写的匹配,自己根据情况进行修改了。

参考文档:http://zhidao.baidu.com/question/111545161.html

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。