十一、String类(lang包底下)
//一个字符串,大写字母个数,小写字母个数,其他字符个数(String类)
public class Helloworld1 {
public static void main(String args[]) {
String str = "360 dazhan QQ";
int num2[] = new int[13];
for(int j=0;j<str.length();j++) {
num2[j] = str.codePointAt(j); //将转化后的ASCII值,传给数组
}
int a = 0;
int b = 0;
int c = 0;
for(int k=0;k<num2.length;k++) {
if(num2[k]>96 && num2[k]<123) {
a++;
}else if(num2[k]>64 && num2[k]<91) {
b++;
}else {
c++;
}
}
System.out.println("小写字母数:"+a);
System.out.println("大写字母数:"+b);
System.out.println("其他字符数:"+c);
}
}
....................................................................................................................................
//给定字符串中出现,某一相同字符串的次数(String类)
public class Helloworld1 {
public static void main(String args[]) {
String str = "Hello world word wo wo";
int count = 0;
int index = 0;
while(str.indexOf("wo",index)>0) { //whlie循环(不知道循环的次数的时候用)
index = str.indexOf("wo",index) + 1; //索引跳过已被找到的字符串
count++;
}
System.out.println("出现字符串wo的次数: "+count);
}
}
浙公网安备 33010602011771号