统计一个字符串中子串出现的次数有以下两种:
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);
对于子字符串的大小写的匹配,自己根据情况进行修改了。
本文介绍了两种统计字符串中子串出现次数的方法:一种是利用正则表达式进行大小不敏感的匹配;另一种是通过将字符串转换为小写并使用split方法进行分割计数。文章还提供了具体的代码示例。
1897

被折叠的 条评论
为什么被折叠?



