您的位置:首页 > 其它

String类练习二

2015-01-14 13:25 162 查看
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: