import java.util.*;import java.io.*;
public class Exp7 {
public static void main(String[] args)throws Exception{
System.out.println("请输入字符串:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
//show(str);
//Scanner scan=new Scanner(System.in);
//String str=scan.next();
String E1="[\u4e00-\u9fa5]";
String E2="[a-zA-Z]";
String E3="[0-9]";
String E4="[\u0020]";
int countH=0;
int countE=0;
int countB=0;
int countN=0;
char[] arrChar=str.toCharArray();
System.out.println("------------------------------------------------");
System.out.println(arrChar);
String[] arrStr=new String[arrChar.length];
for (int i=0;i<arrChar.length ;i++ )
{
arrStr[i]=String.valueOf(arrChar[i]);
}
for(int j=0; j<arrStr.length; j++){
if(arrStr[j].matches(E1))
{countH++;}
if(arrStr[j].matches(E2))
{countE++;}
//if(arrStr[j]==" ")
if(arrStr[j].matches(E4))
{countB++;}
//if(arrStr[j]>"0" && arrStr[j]<"9")
if(arrStr[j].matches(E3))
{countN++;}
}
System.out.println("汉字的个数"+countH);
System.out.println("字母的个数"+countE);
System.out.println("空格的个数"+countB);
System.out.println("数字的个数"+countN);
}
}
下面是程序的执行结果:
请输入字符串:
编程的工作xiyoucanban你 说 呢123 bushiba
------------------------------------------------
编程的工作xiyoucanban你 说 呢123 bushiba
汉字的个数8
字母的个数18
空格的个数3
数字的个数3