一道java笔试题

一次笔试中,考官的题目:

给你一个字符串 String strs ="1234abcd中国威武!";分别区别出 数字、汉字、字符和其他符号等!下面给出一个方法,大家看看!

 1 public class ToStringCount {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         String str ="1234abcd中国威武!";
 9         char[] strToarray = str.toCharArray();
10         for (char ss:strToarray) {            
11             charDistinguish(ss);
12         }
13 
14     }
15 
16     /**
17      * 字符类型识别函数
18      * @param ch
19      */
20         static void charDistinguish(char ch) {   
21          //汉字
22             if(Character.getType(ch) == Character.OTHER_LETTER) {        
23                 System.out.println("汉字:"+ch);            
24             }    
25             //数字
26             else if(Character.isDigit(ch)) {
27                 System.out.println("数字:"+ch);
28             }      
29             //字母
30             else if(Character.isLetter(ch)) {
31                 System.out.println("字符:"+ch);
32             }
33             //其它字符
34             else {
35                 System.out.println("其他:"+ch);        
36             }            
37         }          
38 }

 

posted @ 2012-06-21 13:46  蹲着墙头拉红杏  阅读(434)  评论(1编辑  收藏  举报