本篇再对String类再作一些扩展,包括:
- 字符串数组的遍历
- 各种字符的统计
- 字符串的定义
- char c=newStr.charAt(i);的使用
JavaScript charAt() 方法
定义和用法
charAt() 方法可返回指定位置的字符。
请注意,JavaScript 并没有一种有别于字符串类型的字符数据类型,所以返回的字符是长度为 1 的字符串。
语法
stringObject.charAt(index)
package hello_world;
public class Demo9 {
public static void main(String[] args) {
String str=" aB232 23 &*( s2 ";
String newStr=str.trim();
System.out.println("str="+str);
System.out.println("newStr="+newStr);
int yingWen=0;
int kongGe=0;
int shuzi=0;
int qiTa=0;
for(int i=0;i<newStr.length();i++) {
char c=newStr.charAt(i);
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) {
yingWen++;
System.out.println("英文: "+c);
}else if(c>='0'&&c<='9') {
shuzi++;
System.out.println("数字: "+c);
}else if(c==' ') {
kongGe++;
System.out.println("空格: "+c);
}else {
qiTa++;
System.out.println("其他: "+c);
}
}
}
}
来源:https://blog.csdn.net/qq_36444039/article/details/100899230