java什么求一个子字符串在另一字符串中出现的次数用那个函数?
7个回答
展开全部
基本思想是:用子字符串来作为正则表达式字符串来对给定字符串进行划分,划分后的长度-1即为子字符串的个数。
class Test{
public static void main(String[] args){
String str = "HI,World,I am a programmer, Hello,World,";
String sub = "World";
String[] sps = str.split(sub);
System.out.println("str中共有"+ (sps.length-1) + "个World");
}
}
class Test{
public static void main(String[] args){
String str = "HI,World,I am a programmer, Hello,World,";
String sub = "World";
String[] sps = str.split(sub);
System.out.println("str中共有"+ (sps.length-1) + "个World");
}
}
展开全部
public static void main(String[] args) {
String a = "abcabc23423abc342334abc3453423423";
String b = "abc";
System.out.println(getSubNum(a, b));
}
public static int getSubNum(String a, String b) {
int num = 0;
String str = a;
int index = a.indexOf(b);
while (index != -1) {
num++;
str = str.substring(index + b.length() - 1);
index = str.indexOf(b);
}
return num;
}
String a = "abcabc23423abc342334abc3453423423";
String b = "abc";
System.out.println(getSubNum(a, b));
}
public static int getSubNum(String a, String b) {
int num = 0;
String str = a;
int index = a.indexOf(b);
while (index != -1) {
num++;
str = str.substring(index + b.length() - 1);
index = str.indexOf(b);
}
return num;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-06-19 · 知道合伙人教育行家
关注
展开全部
思路就是从被匹配字符串a中一个一个往后推,截取b字符串长度的字符串:
public class CountHit {
public static void main(String[] args) {
String a = "123456abcde6a6abc6ab";
String b = "6abc";
System.out.println(new CountHit().hit(a, b));
}
/**
*
* @param a
* 被匹配的长字符串
* @param b
* 匹配的短字符串
* @return 匹配次数
*/
public int hit(String a, String b) {
if (a.length() < b.length()) {
return 0;
}
char[] a_t = a.toCharArray();
int count = 0;
for (int i = 0; i < a.length() - b.length(); i++) {
StringBuffer buffer = new StringBuffer();
for (int j = 0; j < b.length(); j++) {
buffer.append(a_t[i + j]);
}
if(buffer.toString().equals(b)){
count ++;
}
}
return count;
}
}
public class CountHit {
public static void main(String[] args) {
String a = "123456abcde6a6abc6ab";
String b = "6abc";
System.out.println(new CountHit().hit(a, b));
}
/**
*
* @param a
* 被匹配的长字符串
* @param b
* 匹配的短字符串
* @return 匹配次数
*/
public int hit(String a, String b) {
if (a.length() < b.length()) {
return 0;
}
char[] a_t = a.toCharArray();
int count = 0;
for (int i = 0; i < a.length() - b.length(); i++) {
StringBuffer buffer = new StringBuffer();
for (int j = 0; j < b.length(); j++) {
buffer.append(a_t[i + j]);
}
if(buffer.toString().equals(b)){
count ++;
}
}
return count;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
现成的方法可能没有
楼上的都能得出结果,但是要需要生成比较多的字符串对象
public static void main(String[] args) throws Exception {
String str = "12345678912345678912313123";
String sub = "123";
System.out.println(find(str, sub));
}
public static int find(String str, String sub) {
int count = 0;
int index = 0;
int temp = 0;
while ((temp = str.indexOf(sub, index)) != -1) {
count++;
index = temp + sub.length();
}
return count;
}
楼上的都能得出结果,但是要需要生成比较多的字符串对象
public static void main(String[] args) throws Exception {
String str = "12345678912345678912313123";
String sub = "123";
System.out.println(find(str, sub));
}
public static int find(String str, String sub) {
int count = 0;
int index = 0;
int temp = 0;
while ((temp = str.indexOf(sub, index)) != -1) {
count++;
index = temp + sub.length();
}
return count;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Demo_1 {
String str = "asd34ad5556dadb5";
public static void main(String[] args){
Demo_1 d = new Demo_1();
System.out.println("ad出现的次数为: " + d.num(d.str));
}
public int num(String str){
int a = str.split("ad").length - 1;
return a;
}
}
String str = "asd34ad5556dadb5";
public static void main(String[] args){
Demo_1 d = new Demo_1();
System.out.println("ad出现的次数为: " + d.num(d.str));
}
public int num(String str){
int a = str.split("ad").length - 1;
return a;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询