任何人都可以建议我在不使用集合的情况下为此编写逻辑。
我有一个字符串s =“这是一个叫做岛屿的地方,它是一个不错的地方”;
输入字符串以查找重复字:“是”;
输出应为:4
答案 0 :(得分:2)
您可以按照以下逻辑进行操作。
stringElement.contains("is")
,则递增在步骤2中创建的计数器
b)如果!stringElement.contains("is")
,则不做任何事情,继续前进到下一个元素。尝试自己编写代码,如果你被困在任何地方,请回到这里。
答案 1 :(得分:0)
string s="This is the place called island and it is a nice place ";
s = s+" ";
System.out.println(s.split("is").length-1);
答案 2 :(得分:0)
就像
一样简单int count = 0;
for (int startIndex = 0; startIndex >= 0; startIndex = str.indexOf("is", startIndex)) {
count++;
}
答案 3 :(得分:0)
使用以下方法,它应该有效:
public static int countSubStrings(String subString, String mainString){
return (mainString.length() - mainString.replace(subString, "").length()) / subString.length();
}
答案 4 :(得分:0)
你可以用它。希望你使用的是Java。
public static void main(String[] args) {
String str = "This is the place called island and it is a nice place";
Pattern pattern = Pattern.compile("is");
Matcher matcher = pattern.matcher(str);
int count = 0;
while (matcher.find())
count++;
System.out.println(count); // prints 4
}
答案 5 :(得分:0)
此方法将计算s1中出现的s2次数
public static int CountStr(String s1,String s2){
int cnt=0,temp=0;
if(s1.indexOf(s2)>0){
temp=s1.indexOf(s2);
cnt++;
}
while(s1.indexOf(s2,temp+1)>0){
cnt++;
temp=s1.indexOf(s2,temp+1);
}
return cnt;
}