java怎么实现统计一个字符串中字符出现的次数
2个回答
展开全部
package com.zuidaima.util.string;
import java.io.*;
public class CountString {
public static int count(String result, String target) {
int count = 0;
int index = 0;
while (true) {
index = result.indexOf(target, index + 1);
if (index > 0) {
count++;
} else {
break;
}
}
br.close();
return count;
}
public static void main(String[] args) {
try {
System.out.println(count("asdajklsdjklasjdkalsj", "a"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
public class CountString {
public static int count(String result, String target) {
int count = 0;
int index = 0;
while (true) {
index = result.indexOf(target, index + 1);
if (index > 0) {
count++;
} else {
break;
}
}
br.close();
return count;
}
public static void main(String[] args) {
try {
System.out.println(count("asdajklsdjklasjdkalsj", "a"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2017-02-07
展开全部
String str1 = "nihaoksdoksad";
String str2 = "ok";
int total = 0;
for (int i = 0; i < str1.length(); i ++){
if(str1.indexOf(str2) == 0){
total ++;
}
}
System.out.println(str1+"中含有"+total+"个"+str2);
String str2 = "ok";
int total = 0;
for (int i = 0; i < str1.length(); i ++){
if(str1.indexOf(str2) == 0){
total ++;
}
}
System.out.println(str1+"中含有"+total+"个"+str2);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询