比word先进的字数统计 
我测试过了,基本接近完美,但是肯定还有BUG 望提出!谢谢 package taotao.montao.demo;/**
 * 获取文章的字数或则字符数
 * @author montao
 */
public class StatWordCount {

private final char[] CHS = {',',';','.','!','?',';','+','。','?','!'};  //符号数组

private final char[] CHN = {'\n','\r'}; //转义符数组

private final char[] SPACE = {' ',' '}; //空格的数组(前半角,后全角)

/**
 * 根据指定条件来筛选文章的字数
 * @param wordContent  文章内容
 * @param compriseInterpunction  是否包含指定字符
 * @param compriseSpace  是否包含空格
 * @return 返回文章经过指定筛选后的长度
 */
public int getWordCount(String wordContent,boolean compriseInterpunction,boolean compriseSpace)
{
if(wordContent==null){
return 0;
}else if(wordContent.length()==0){
return 0; 
}else{
//既要包含符号又要包含空格
if(compriseInterpunction && compriseSpace){
//清除转义符
String regex = "["+new String(CHN)+"]";
wordContent = wordContent.replaceAll(regex," ");
return this.getWordCount(wordContent);
}
//不包含符号包含空格
else if(!compriseInterpunction && compriseSpace){
//使用正则表达式去掉指定的符号和转义符
String regex1 = "["+new String(CHN)+"]";
String regex2 = "["+new String(CHS)+"]";
wordContent = wordContent.replaceAll(regex1," ");
wordContent = wordContent.replaceAll(regex2," ");
return this.getWordCount(wordContent);
}
//包含指定符号不包含空格
else if(compriseInterpunction && !compriseSpace){
//使用正则表达式去掉空格和转义符
String regex1 = "["+new String(CHN)+"]";
String regex2 = "["+new String(SPACE)+"]";
wordContent = wordContent.replaceAll(regex1," ");
wordContent = wordContent.replaceAll(regex2," ");
return this.getWordCount(wordContent);
}
//空格和指定符号都不包含
else{
//使用正则表达式去掉空格,指定符号和转义符
String regex1 = "["+new String(CHN)+"]";
String regex3 = "["+new String(CHS)+"]";
String regex2 = "["+new String(SPACE)+"]";
wordContent = wordContent.replaceAll(regex1," ");
wordContent = wordContent.replaceAll(regex2," ");
wordContent = wordContent.replaceAll(regex3," ");
return this.getWordCount(wordContent);
}
}
}

/**
 * 返回文章中的字数
 * @param wordCount 文章内容
 * @return
 */
@SuppressWarnings("unused")
private int getWordCount(String wordContent){
int count = 0;
if(wordContent==null){ //判断是否为null,如果为null直接返回0
count = 0;
}else if(wordContent.length()==0){ //判断是否为空,如果为空直接返回0
count = 0;
}else{ //判断获取字数
wordContent = wordContent.trim(); //清空空格
//临时变量
String s4 = "";
String s3 = "";
String s1 = "";
boolean bb = false;
if(wordContent.length()>0){
s4 = String.valueOf(wordContent.charAt(wordContent.length()-1));
}
for(int i=0;i<wordContent.length();i++){
s3 = String.valueOf(wordContent.charAt(i));
int num = s3.getBytes().length;
if(s3.hashCode()==32||s3.getBytes().length==2){
bb=true;
}if(num==2){
count++;
}else{
if(i+1<wordContent.length()&&(i>1)){
s1 = String.valueOf(wordContent.charAt(i+1));
if((s1.hashCode()==32||s1.getBytes().length==2)&&(s3.hashCode()!=32)){
count++;
}
}
}
}
if(!bb){
count++;
}else{
if(s4.getBytes().length==1){
count++;
}
}
}
return count;
}

/**
 * 根据条件来获取文章的字符数
 * @param wordContent 文章内容
 * @param compriseInterpunction 是否包含指定符号
 * @param compriseSpace 是否包含空格
 * @return 返回字符长度
 */
public int getWordCharacter(String wordContent,boolean compriseInterpunction,boolean compriseSpace)
{
//既要包含符号又要包含空格
if(compriseInterpunction && compriseSpace){
//清除转义符
String regex = "["+new String(CHN)+"]";
wordContent = wordContent.replaceAll(regex," ");
//首部的空格不算
wordContent = wordContent.replaceAll("^\\s+","");
return wordContent.length();
}//不包含符号包含空格
else if(!compriseInterpunction && compriseSpace){
//首部的空格不算
wordContent = wordContent.replaceAll("^\\s+","");
//使用正则表达式去掉指定的符号和转义符
String regex1 = "["+new String(CHN)+"]";
String regex2 = "["+new String(CHS)+"]";
wordContent = wordContent.replaceAll(regex1," ");
wordContent = wordContent.replaceAll(regex2," ");
return wordContent.length();
}//包含指定符号不包含空格
else if(compriseInterpunction && !compriseSpace){
//使用正则表达式去掉空格和转义符
return this.getNoSpaceCount(wordContent);
}//空格和指定符号都不包含
else{
//使用正则表达式去掉指定符号
String regex1 = "["+new String(CHS)+"]";
wordContent = wordContent.replaceAll(regex1," ");
return this.getNoSpaceCount(wordContent);
}
} /**
 * 获取文章中非空格的字符总数
 * @param wordContent 文章内容
 * @return
 */
private int getNoSpaceCount(String wordContent) {
int spaceCount = 0;
if(wordContent==null)
{
spaceCount = 0;
}else if(wordContent.length()==0)
{
spaceCount = 0;
}else
{
//替换首部的
wordContent = wordContent.replaceAll("^\\s+","");
wordContent = wordContent.replaceAll(" ","");
//使用正则替换转义符
String regex = "["+new String(CHN)+"]";
wordContent = wordContent.replaceAll(regex,"");
spaceCount = wordContent.length();
}
return spaceCount;
}
}

解决方案 »

  1.   

        StatWordCount o = new StatWordCount();
        String str = " is www enjoyjava net";
        
        System.out.println(o.getWordCount(str,false,false));返回 3我无论怎样都敢搞不懂,还是有bug
      

  2.   


    WordCount wordCount = new WordCount();
         String str = " is www enjoyjava net";
        
         System.out.println(wordCount.getWordCharacter(str, false, false));是17啊
      

  3.   

    我觉得一定是不完美的如果word中出现了图片,或者是日本字符==还会返回正确值吗倒不如  split("[ , .\n\r]")算了
      

  4.   

    给你顶顶
    我没 JAVA软件
    收了也没用吧
      

  5.   

    你丫除了水贴也会收藏这么有含量的技术贴?
    ===============================================
    这段代码有问题很大
    我用的是日文的xp系统。公司里。
    完全错的一塌糊涂哦
    随便找了篇word都和他本身的统计字数差很远
      

  6.   

    或许你不支持吧(@#&)@#×@×#——@(#——¥@(——
      

  7.   

    能解释一下这段的思路吗?
      if(s3.hashCode()==32||s3.getBytes().length==2){
                        bb=true;
                    }if(num==2){
                        count++;
                    }else{
                         if(i+1<wordContent.length()&&(i>1)){
                              s1 = String.valueOf(wordContent.charAt(i+1));
                             if((s1.hashCode()==32||s1.getBytes().length==2)&&(s3.hashCode()!=32)){
                                    count++;
                              }
                          }
                    }
                }
      

  8.   

    最新版本,没有BUG了! 要支持什么符号,可以自己在数组中加入阿!很多人不知道这个的用处,我们做报社项目,客户刊登文字广告是按照字数来计费的,但是有时候又不会包含特定符号和空格,所以列出了8种情况!我测试过处理8W多字的文章2″搞定,呵呵 可能会与机器有关吧!
    package taotao.montao.demo.util;/**
     * 获取文章的字数或则字符数
     * @author montao
     */
    public class StatWordCount {
        
        private final char[] CHS = {',',';','!','.','!','?',';','+',',','?','!','/'};  //符号数组
        
        private final char[] CHN = {'\r','\n'}; //转义符数组
        
        private final char[] SPACE = {' ',' '}; //空格的数组(前半角,后全角)
        
        /**
         * 根据指定条件来筛选文章的字数
         * @param wordContent  文章内容
         * @param compriseInterpunction  是否包含指定字符
         * @param compriseSpace  是否包含空格
         * @return 返回文章经过指定筛选后的长度
         */
        public int getWordCount(String wordContent,boolean compriseInterpunction,boolean compriseSpace)
        {
            if(wordContent==null){
                return 0;
            }else if(wordContent.length()==0){
                return 0; 
            }else{
                //既要包含符号又要包含空格
                if(compriseInterpunction && compriseSpace){
                    //清除转义符
                    String regex = "["+new String(CHN)+"]";
                    wordContent = wordContent.replaceAll(regex," ");
                    return this.getWordCount(wordContent);
                }
                //不包含符号包含空格
                else if(!compriseInterpunction && compriseSpace){
                    //使用正则表达式去掉指定的符号和转义符
                    String regex1 = "["+new String(CHN)+"]";
                    String regex2 = "["+new String(CHS)+"]";
                    wordContent = wordContent.replaceAll(regex1," ");
                    wordContent = wordContent.replaceAll(regex2," ");
                    return this.getWordCount(wordContent);
                }
                //包含指定符号不包含空格
                else if(compriseInterpunction && !compriseSpace){
                    //使用正则表达式去掉空格和转义符
                    String regex1 = "["+new String(CHN)+"]";
                    String regex2 = "["+new String(SPACE)+"]";
                    wordContent = wordContent.replaceAll(regex1," ");
                    wordContent = wordContent.replaceAll(regex2," ");
                    return this.getWordCount(wordContent);
                }
                //空格和指定符号都不包含
                else{
                    //使用正则表达式去掉空格,指定符号和转义符
                    String regex1 = "["+new String(CHN)+"]";
                    String regex3 = "["+new String(CHS)+"]";
                    String regex2 = "["+new String(SPACE)+"]";
                    wordContent = wordContent.replaceAll(regex1," ");
                    wordContent = wordContent.replaceAll(regex2," ");
                    wordContent = wordContent.replaceAll(regex3," ");
                    return this.getWordCount(wordContent);
                }
            }
        }
        
        /**
         * 返回文章中的字数
         * @param wordCount 文章内容
         * @return
         */
        @SuppressWarnings("unused")
        private int getWordCount(String wordContent){
            int count = 0;
            if(wordContent==null){ //判断是否为null,如果为null直接返回0
                count = 0;
            }else if(wordContent.length()==0){ //判断是否为空,如果为空直接返回0
                count = 0;
            }else{ //判断获取字数
                wordContent = wordContent.trim(); //清空空格
                //临时变量
                String s4 = "";
                String s3 = "";
                String s2 = "";
                String s1 = "";
                boolean bb = false;
                if(wordContent.length()>0){
                    s4 = String.valueOf(wordContent.charAt(wordContent.length()-1));
                }
                for(int i=0;i<wordContent.length();i++){
                    s3 = String.valueOf(wordContent.charAt(i));
                    int num = s3.getBytes().length;
                    if(s3.hashCode()==32||s3.getBytes().length==2){
                        bb=true;
                    }if(num==2){
                        count++;
                    }else{
                            if(i+1<wordContent.length()){
                                s1 = String.valueOf(wordContent.charAt(i+1));
                                if((s1.hashCode()==32 && (s3.hashCode()!=32)) || ((s1.getBytes().length==2)&& (s3.hashCode()!=32))){
                                    count++;
                                }
                            }
                    }
                }
                if(!bb){
                    count++;
                }else{
                    if(s4.getBytes().length==1){
                        count++;
                    }
                }
            }
            return count;
        }
        
        /**
         * 根据条件来获取文章的字符数
         * @param wordContent 文章内容
         * @param compriseInterpunction 是否包含指定符号
         * @param compriseSpace 是否包含空格
         * @return 返回字符长度
         */
        public int getWordCharacter(String wordContent,boolean compriseInterpunction,boolean compriseSpace)
        {
            //既要包含符号又要包含空格
            if(compriseInterpunction && compriseSpace){
                //清除转义符
                String regex = "["+new String(CHN)+"]";
                wordContent = wordContent.replaceAll(regex,"");
                //首部的空格不算
                wordContent = wordContent.replaceAll("^\\s+","");
                return wordContent.length();
            }//不包含符号包含空格
            else if(!compriseInterpunction && compriseSpace){
                //首部的空格不算
                wordContent = wordContent.replaceAll("^\\s+","");
                //使用正则表达式去掉指定的符号和转义符
                String regex1 = "["+new String(CHN)+"]";
                String regex2 = "["+new String(CHS)+"]";
                wordContent = wordContent.replaceAll(regex1,"");
                wordContent = wordContent.replaceAll(regex2,"");
                return wordContent.length();
            }//包含指定符号不包含空格
            else if(compriseInterpunction && !compriseSpace){
                //使用正则表达式去掉空格和转义符
                String regex = "["+new String(SPACE)+"]";
                wordContent = wordContent.replaceAll(regex," ");
                return this.getNoSpaceCount(wordContent);
            }//空格和指定符号都不包含
            else{
                //使用正则表达式去掉指定符号
                String regex1 = "["+new String(CHS)+"]";
                String regex2 = "["+new String(SPACE)+"]";
                wordContent = wordContent.replaceAll(regex1," ");
                wordContent = wordContent.replaceAll(regex2," ");
                return this.getNoSpaceCount(wordContent);
            }
        }    /**
         * 获取文章中非空格的字符总数
         * @param wordContent 文章内容
         * @return
         */
        private int getNoSpaceCount(String wordContent) {
            int spaceCount = 0;
            if(wordContent==null)
            {
                spaceCount = 0;
            }else if(wordContent.length()==0)
            {
                spaceCount = 0;
            }else
            {
                //替换首部的
                wordContent = wordContent.replaceAll("^\\s+","");
                wordContent = wordContent.replaceAll(" ","");
                //使用正则替换转义符
                String regex = "["+new String(CHN)+"]";
                wordContent = wordContent.replaceAll(regex,"");
                spaceCount = wordContent.length();
            }
            return spaceCount;
        }
    }
      

  9.   

    功能可以,不过代码....有点点不好~~~~~hoho~~~~
      

  10.   

    顺便再添一句,这样用if else  不好~~~~
      

  11.   

    最好改成下面这样
         /**
         * 根据指定条件来筛选文章的字数
         * @param wordContent  文章内容
         * @param compriseInterpunction  是否包含指定字符
         * @param compriseSpace  是否包含空格
         * @return 返回文章经过指定筛选后的长度
         */
        public int getWordCount(String wordContent,boolean compriseInterpunction,boolean compriseSpace)
        {
            if(wordContent==null){
                return 0;
            if(wordContent.length()==0){
                return 0; 
          
            //既要包含符号又要包含空格
             if(compriseInterpunction && compriseSpace){
                    //清除转义符
                    String regex = "["+new String(CHN)+"]";
                    wordContent = wordContent.replaceAll(regex," ");
                    return this.getWordCount(wordContent);
                }
             //不包含符号包含空格
               if(!compriseInterpunction && compriseSpace){
                    //使用正则表达式去掉指定的符号和转义符
                    String regex1 = "["+new String(CHN)+"]";
                    String regex2 = "["+new String(CHS)+"]";
                    wordContent = wordContent.replaceAll(regex1," ");
                    wordContent = wordContent.replaceAll(regex2," ");
                    return this.getWordCount(wordContent);
                }
             //包含指定符号不包含空格
              if(compriseInterpunction && !compriseSpace){
                    //使用正则表达式去掉空格和转义符
                    String regex1 = "["+new String(CHN)+"]";
                    String regex2 = "["+new String(SPACE)+"]";
                    wordContent = wordContent.replaceAll(regex1," ");
                    wordContent = wordContent.replaceAll(regex2," ");
                    return this.getWordCount(wordContent);
                }
                //空格和指定符号都不包含
                else{
                    //使用正则表达式去掉空格,指定符号和转义符
                    String regex1 = "["+new String(CHN)+"]";
                    String regex3 = "["+new String(CHS)+"]";
                    String regex2 = "["+new String(SPACE)+"]";
                    wordContent = wordContent.replaceAll(regex1," ");
                    wordContent = wordContent.replaceAll(regex2," ");
                    wordContent = wordContent.replaceAll(regex3," ");
                    return this.getWordCount(wordContent);
                }
            }
        }编代码就要简洁,明了 ,顺便再添一句,还要要考虑内存的使用  LZ不要自恋了哦
      

  12.   

    上面的修改错了,下面的才对的~~~不好意思  /**
         * 根据指定条件来筛选文章的字数
         * @param wordContent  文章内容
         * @param compriseInterpunction  是否包含指定字符
         * @param compriseSpace  是否包含空格
         * @return 返回文章经过指定筛选后的长度
         */
        public int getWordCount(String wordContent,boolean compriseInterpunction,boolean compriseSpace)
        {
            if(wordContent==null)
                return 0;
            if(wordContent.length()==0)
                return 0; 
                       //既要包含符号又要包含空格
             if(compriseInterpunction && compriseSpace){
                    //清除转义符
                    String regex = "["+new String(CHN)+"]";
                    wordContent = wordContent.replaceAll(regex," ");
                    return this.getWordCount(wordContent);
                }
                //不包含符号包含空格
               if(!compriseInterpunction && compriseSpace){
                    //使用正则表达式去掉指定的符号和转义符
                    String regex1 = "["+new String(CHN)+"]";
                    String regex2 = "["+new String(CHS)+"]";
                    wordContent = wordContent.replaceAll(regex1," ");
                    wordContent = wordContent.replaceAll(regex2," ");
                    return this.getWordCount(wordContent);
                }
                //包含指定符号不包含空格
               if(compriseInterpunction && !compriseSpace){
                    //使用正则表达式去掉空格和转义符
                    String regex1 = "["+new String(CHN)+"]";
                    String regex2 = "["+new String(SPACE)+"]";
                    wordContent = wordContent.replaceAll(regex1," ");
                    wordContent = wordContent.replaceAll(regex2," ");
                    return this.getWordCount(wordContent);
                }
                //空格和指定符号都不包含
                else{
                    //使用正则表达式去掉空格,指定符号和转义符
                    String regex1 = "["+new String(CHN)+"]";
                    String regex3 = "["+new String(CHS)+"]";
                    String regex2 = "["+new String(SPACE)+"]";
                    wordContent = wordContent.replaceAll(regex1," ");
                    wordContent = wordContent.replaceAll(regex2," ");
                    wordContent = wordContent.replaceAll(regex3," ");
                    return this.getWordCount(wordContent);
                }
            }
        }
      

  13.   

    好东西 
    感谢楼主
    不过现在还没有学习JAVA  先留着了 呵呵
      

  14.   

    public static void main(String[] args){
    StatWordCount o = new StatWordCount();
        String str = " is www enjoyjava net";
        System.out.println(o.getWordCount(str,false,false));
    }
    一直返回4