求助 如何得到 一个字符串所含另一个子串的个数

inter1 2007-03-01 01:56:38
比如,
str1 = "acdacd yyyojo acdadd"
中含有 acd的个数,
结果应该是3。

先谢了,string中找不到相关的方法。
...全文
314 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
新街IT男 2008-12-28
  • 打赏
  • 举报
回复
* StringUtils.countMatches(null, *) = 0
* StringUtils.countMatches("", *) = 0
* StringUtils.countMatches("abba", null) = 0
* StringUtils.countMatches("abba", "") = 0
* StringUtils.countMatches("abba", "a") = 2
* StringUtils.countMatches("abba", "ab") = 1
* StringUtils.countMatches("abba", "xxx") = 0

public static int countMatches(String str, String sub) {
if (isEmpty(str) || isEmpty(sub)) {
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
count++;
idx += sub.length();
}
return count;
}
gongyali2005 2007-03-01
  • 打赏
  • 举报
回复
用正则吧.
wwyyhhww 2007-03-01
  • 打赏
  • 举报
回复
split是个正则表达式,按照acd把你原来的字符串拆成多个字符串,然后你在进行判断,很简单的
shine333 2007-03-01
  • 打赏
  • 举报
回复
public static int countMatches(String str, String sub) {
if (isEmpty(str) || isEmpty(sub)) {
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
count++;
idx += sub.length();
}
return count;
}

public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}

emin_lee 2007-03-01
  • 打赏
  • 举报
回复
mark!
wanguanghai 2007-03-01
  • 打赏
  • 举报
回复
用substring 从要查找的串中写个
「已注销」 2007-03-01
  • 打赏
  • 举报
回复
再修正

if(str.equals(substring))
return 1;
shine333 2007-03-01
  • 打赏
  • 举报
回复
问一下,"aaaaaa"中包含多少个"aa"
3个,即
"aa...."
"..aa.."
"....aa"
还是5个
"aa...."
".aa..."
"..aa.."
"...aa."
"....aa"

建议用apache的jakarta commons lang包的
StringUtils.countMatches()

StringUtils.countMatches(null, *) = 0
StringUtils.countMatches("", *) = 0
StringUtils.countMatches("abba", null) = 0
StringUtils.countMatches("abba", "") = 0
StringUtils.countMatches("abba", "a") = 2
StringUtils.countMatches("abba", "ab") = 1
StringUtils.countMatches("abba", "xxx") = 0
StringUtils.countMatches("aaaaaa", "aa") = 3

「已注销」 2007-03-01
  • 打赏
  • 举报
回复
看了下,原来是split的问题
修正如下:

static int getSubStringLength(String str,String substring)
{
int 重复次数=str.split(substring).length-1;
if(str.endsWith(substring))
重复次数++;
return 重复次数;
}
inter1 2007-03-01
  • 打赏
  • 举报
回复
楼上的 如果 字符串 是 acd的时候就不对了
「已注销」 2007-03-01
  • 打赏
  • 举报
回复
str1.split("acd").length-1
课程介绍:第一章:正则表达式(regularexpression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。第二章:http协议是一种无状态协议,不记录用户行为,我们可以利用cookie记录数据,方便用户操作,提升用户体验。第三章:ECMAScript6.0(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了。它的目标,是使得JavaScript语言可以用来编写复杂的大型应用程序,成为企业级开发语言。第四章:本章主要讲解JS动画原理、动画函数封装和轮播。第五章:本章主要讲解面向对象、构造函数和继承、原型链和继承。第六章:本节课程主要讲解了什么是Ajax、如何使用Ajax发送get请求、如何使用Ajax发送post请求、JSON数据格式、回调地狱、Promise和Ajax的同源策略、跨域请求。第七章:本章主要讲解html、val、attr、prop、class、全选框、动画、节点遍历、ajax、sonp、event、multiple、plugin、plugin、magnifier。第八章:本章主要讲解UML类图、单例模式、工厂模式、策略模式、代理模式、观察者模式。第九章:本章主要讲解为什么要模块化、原生JS中,模块的写法、AMD、CommonJS&Webpack。第十章:本节课程主要讲解了服务器安装环境配置、端口及ip基本常识、简单认识PHP(helloworld)、基本语法和动态网页原理。第十一章:本节课程主要讲解了什么是SASS、SASS的预处理、ass语法(变量、嵌套、导入、mixin、扩展、function、expression)。第十二章:本节课程主要讲解了什么是GULP、GULP环境配置、GULP基本使用及GULP的插件安装与使用。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧