java学习----String类拓展

耗尽温柔 提交于 2019-11-29 19:31:06

本篇再对String类再作一些扩展,包括:

  1. 字符串数组的遍历
  2. 各种字符的统计
  3. 字符串的定义
  4. 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);
			}
		}
		
	}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!