/** * Created by gao.mq on 2017/5/31. */ public class CaculateCharCount { //空格个数 private static int blankCount=0; //字符个数 private static int chCount=0; //数字个数 private static int digitCount=0; //其他个数 private static int otherCount=0; public static void main(String[] args){ String str="我是高明乾',Welcome To China! 123"; for (int i=0;i<str.length();i++){ char c= str.charAt(i); if(c==' '){ blankCount++; } else if((c>='a'&&c<='z')||('A'<=c&&c<='Z')){ chCount++; } else if(c>='0'&&c<='9'){ digitCount++; } else { otherCount++; } } System.out.println("空格:"+blankCount+"字符:"+chCount+"数字:"+digitCount+"其他:"+otherCount); } }
转载于:https://my.oschina.net/gaomq/blog/911872