package com.jenthor;
public class StringGet {
/*
* 获取一个字符串在另一个字符串中出现的次数 例:"abkkwjkejkkauukkdhkk", "kk"字符串出现的次数。
*/
public static void main(String[] args) {
String s = "abkkwjkejkkauukkdhkk";
String key = "kk";
int x = getcount(s, key);
System.out.println(x);
}
public static int getcount(String str, String key) {
int count = 0;
int index = 0;
while (str.indexOf(key,index)!=-1) {
index = str.indexOf(key,index) + key.length();
count++;
}
return count;
}
}
String类练习二
最新推荐文章于 2023-02-09 20:18:01 发布