package gct.countMain;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Count {
public int count(String toCount){
/*
* 汉字字符包含2字节,英文字符1字节,中文标点2字节,英文标点1字节
*/
int shuziNum,zimuNum,symbolNum,otherNum,higherNum,hanziNum,hanziBD;//分别表示数字、字母、英文标点及其他字符个数
shuziNum=zimuNum=symbolNum=otherNum=higherNum=hanziNum=hanziBD=0;
for(int i=0;i<toCount.length();i++){
char c=toCount.charAt(i);
if(c>=48 && c<=57){ //acsii中数字范围是48-59
shuziNum++;
}else if((c>=65 && c<=90) || (c>=97 && c<=122)){ //大写字母65-90,小写字母97-122
zimuNum++;
}else if((c>=33 && c<=47)||(c>=58 && c<=64)||(c>=91 && c<=96) ||(c>=123 &&c<=126)){
symbolNum++;
}
else if((c>=0 && c<=32)||(c==127)){
otherNum++;
}
else if(c>=128 &&c<=255){
higherNum++;
}
}
/*
System.out.println("数字个数: "+shuziNum);
System.out.println("字母个数: "+zimuNum);
System.out.println("英文标点个数: "+symbolNum);
System.out.println("其他个数: "+otherNum);
System.out.println("双字节字符个数: "+higherNum);
*/
{
String regex="[\u4E00-\u9FA5]";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(toCount);
//System.out.println("提取出来的中文有: ");
while(m.find()){
hanziNum++;
//System.out.print(m.group()+" ");
}
}
/*
System.out.println();
System.out.println("汉字的出现个数: "+hanziNum);*/
{
String regex="[·~!#¥%……&*()——+|【】{}“’:;《》,。?、]";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(toCount);
while(m.find()) {
hanziBD++;
//System.out.print(m.group()+" ");
}
//System.out.println();
//System.out.println("汉字字符的出现个数: "+hanziBD);
}
return shuziNum*1+zimuNum*1+symbolNum*1+otherNum*1+higherNum*1+hanziNum*2+hanziBD*2;
}
}
评论3
最新资源