如何统计字符串中某字符的个数
答案:3 悬赏:0 手机版
解决时间 2021-02-27 15:37
- 提问者网友:抽煙菂渘情少年
- 2021-02-26 15:15
如何统计字符串中某字符的个数
最佳答案
- 五星知识达人网友:掌灯师
- 2021-02-26 15:23
public class StrTest {
public static int f(String s1,String s2){
int a = s1.length();
int b = s2.length();
int i = 0,sum = 0;
while( i <= (b - a)){
if(s1.charAt(0) == s2.charAt(i)){
if(s2.substring(i, i + a ).equals(s1))
{i = i + a - 1 ;
sum++;
}
}
++i;
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入子字符串");
String s1 = sc.next();
System.out.println("输入母字符串");
String s2 = sc.next();
System.out.println("子字符串在母字符串中出现的次数是:" + f(s1,s2));
}
}
全部回答
- 1楼网友:平生事
- 2021-02-26 17:28
String str = "abcde123" ;
int count = 0 ;
for (int i = 0;i<str.length();i++){
if(str.substring(i,i+1).equals("a")){
count++;
}
}
System.out.println(count);
- 2楼网友:woshuo
- 2021-02-26 15:56
private sub command1_click()
dim w as integer, n as integer, t as integer '我你他的个数
for i = 1 to len(text1)
select case mid(text1, i, 1)
case "我"
w = w + 1
case "你"
n = n + 1
case "他"
t = t + 1
end select
next
text2 = "我" & w & "个,你" & n & "个,他" & t & "个"
end sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯