java 实现仿word2007字数统计功能

近期做一个阅读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`\\-=\';.,/~!@#$%^&*()_+|}{\":>

 

二:调用 调用的话,非常简单,调方法,传参数即可。

String str="xxxxxxxx进枯时进田团团圆圆大厅大规模";
int count=getMSWordsCount(str);

原链接

你可能感兴趣的:(java)