JAVA如何统计字符串的中、英文字符数量(中文、英文)(打印控制、数据量统计)

来源:互联网 发布:淘宝珍珠内裤买家秀 编辑:程序博客网 时间:2024/04/27 23:10
直接上代码了,不说其他的了,有问题或更好的方案,请留言交流,谢谢!!
public void onClick(View v) {// 要统计的字符串String text = etTest.getText().toString().trim();// 字符串的字符数量int totalLength = text.length();// 字符串中中文(含中文符合)字符数量int chCharsLength = (text.getBytes().length - totalLength) / 2;// 字符串中英文(含符号)、数字字符数量int enCharsLength = totalLength - chCharsLength;tvResult.setText("总字符数:" + totalLength + "\n中文字符数:"+ chCharsLength + "\n英文字符数" + enCharsLength);   }


0 0