求助 如何得到 一个字符串所含另一个子串的个数 比如,str1 = "acdacd yyyojo acdadd"中含有 acd的个数,结果应该是3。先谢了,string中找不到相关的方法。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼上的 如果 字符串 是 acd的时候就不对了 看了下,原来是split的问题修正如下:static int getSubStringLength(String str,String substring){ int 重复次数=str.split(substring).length-1; if(str.endsWith(substring)) 重复次数++; return 重复次数;} 问一下,"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 再修正if(str.equals(substring)) return 1; 用substring 从要查找的串中写个 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; } split是个正则表达式,按照acd把你原来的字符串拆成多个字符串,然后你在进行判断,很简单的 * 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") = 0public 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; } 如何将图像变成字符 还是概念问题搞不清楚,昏死,想自杀啊! 关于poi 的研究 JDBC连接oracle的问题 sun网站的swing教材,最强链接 为什么JSplitPane只能往左移,不能往右移 面试时被问到一个问题....请教... 求一 jdbc连接DB2的应用程序例子!急! 100分轻而易得 JBuilder6建Entity Bean的一些初级问题! JAVA中如何调用其他类文件中的对象实例。。 我想自学java请大家给一个时间表
修正如下:static int getSubStringLength(String str,String substring)
{
int 重复次数=str.split(substring).length-1;
if(str.endsWith(substring))
重复次数++;
return 重复次数;
}
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
return 1;
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;
}
* 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") = 0public 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;
}