8.22 字符串统计 toCharArray
统计字符串中"n","o"出现的次数
class StringUtil {
// 返回第一个内容为字母n的个数,第二个内容为字母o的个数
public static int[] count(String str){
int countData[] = new int[2];
char[] data = str.toCharArray();//将字符串变成字符数组,其中空格也占位
for(int x = 0;x < data.length;x++){
//System.out.print(data[x]);//want you to know one thing
if(data[x] == 'n' || data[x] == 'N'){
countData[0]++;
}
if(data[x] == 'o' || data[x] == 'O'){
countData[1]++;
}
}
return countData;
}
}
public class HelloWorld {
public static void main(String args[]) {
String str = "want you to know one thing";
int result[] = StringUtil.count(str);
System.out.println("字母n的个数:" + result[0]);
System.out.println("字母o的个数:" + result[1]);
}
}
相信坚持的力量,日复一日的习惯.

浙公网安备 33010602011771号