近期做一个阅读app,需要统计字符,为了与word保持一致,在网上找了一番,最终找到一个可以使用的,贡献给大家。
/**
* 类似word统计字符数
*
* @param context
* @return
*/
public static int getMSWordsCount(String context) {
int words_count = 0;
//中文单词
String cn_words = context.replaceAll("[^(\\u4e00-\\u9fa5,。《》?;’‘:“”【】、)(……¥!·)]", "");
int cn_words_count = cn_words.length();
//非中文单词
String non_cn_words = context.replaceAll("[^(a-zA-Z0-9`\\-=\';.,/~!@#$%^&*()_+|}{\":><?\\[\\])]", " ");
int non_cn_words_count = 0;
String[] ss = non_cn_words.split(" ");
for (String s : ss) {
if (s.trim().length() != 0) non_cn_words_count++;
}
//中文和非中文单词合计
words_count = cn_words_count + non_cn_words_count;
// ToolLog.d(ConstString.TAG, "汉字:" + cn_words_count + "\n\t字符:" + non_cn_words_count);
return words_count;
}
二:调用 调用的话,非常简单,调方法,传参数即可。
String str="xxxxxxxx进枯时进田团团圆圆大厅大规模";
int count=getMSWordsCount(str);
原链接