// 默認一個字符串
String str = "我愛你中國,我愛你故鄉";
// 聲明一個數組
String[] strArray = new String[100];
// 聲明一個變量計數
int count = 0;
for (int i = 0; i < str.length(); i++) {
// 截取一個字符,存到數組中
strArray[i] = str.substring(i, i + 1);
if (strArray[i] != null) {
if (strArray[i].equals("愛")) {
count++;
}
System.out.println(strArray[i]);
}
}
System.out.println("愛字出現" + count + "次");
String str = "我愛你中國,我愛你故鄉";
// 聲明一個數組
String[] strArray = new String[100];
// 聲明一個變量計數
int count = 0;
for (int i = 0; i < str.length(); i++) {
// 截取一個字符,存到數組中
strArray[i] = str.substring(i, i + 1);
if (strArray[i] != null) {
if (strArray[i].equals("愛")) {
count++;
}
System.out.println(strArray[i]);
}
}
System.out.println("愛字出現" + count + "次");