1、Character 类在对象中包装一个基本类型 char 的值
此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然
构造方法:
Character(char value)
1 public classCharacterDemo {2 public static voidmain(String[] args) {3 //创建对象4 //Character ch = new Character((char) 97);
5 Character ch = new Character('a');6 System.out.println("ch:" +ch);7 }8 }
2、 Character的几个方法:
1、public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符
2、public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
3、public static boolean isDigit(char ch):判断给定的字符是否是数字字符
4、public static char toUpperCase(char ch):把给定的字符转换为大写字符
5、Public static char toLowerCase(char ch):把给定的字符转换为小写字符
3、 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
分析:
A、键盘录入字符串
B、定义3个int变量:smallcount、bigcount、numbercount
C、把字符串转换成数组,并进行遍历判断
a、字符为小写字母:smllcount++
b、字符为大写字母:bigcount++
c、字符为数字:numbercount++
D、输出结果
E、写成方法:
a、返回类型:void 直接打印出结果
b、参数列表:string str
1 importjava.util.Scanner;2 public classCharacterTest1 {3
4 public static voidmain(String[] args) {5 //创建键盘录入
6 Scanner sc = newScanner(System.in);7 System.out.println("请输入字符串:");8 String str =sc.nextLine();9
10 //调用方法
11 choose(str);12 }13
14 //定义方法
15 public static voidchoose(String str){16 //定义3个变量
17 int smallcount = 0;18 int bigcount = 0;19 int numbercount = 0;20 //把字符串转换成数组
21 char [] ch =str.toCharArray();22 //对数组进行遍历和判断
23 for(int x = 0; x < ch.length ; x ++){24 //public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
25 if(Character.isLowerCase(ch[x])){26 smallcount ++;27 }28 else if(Character.isUpperCase(ch[x])){29 bigcount ++;30 }31 else if(Character.isDigit(ch[x])){32 numbercount ++;33 }34 }35 System.out.println("字符串里的小写字母一共有"+smallcount+"个");36 System.out.println("字符串里的大写字母一共有"+bigcount+"个");37 System.out.println("字符串里的数字一共有"+numbercount+"个");38 }39
40 }