計算文章字數

 

  1. package taotao.montao.demo.util; 
  2.  
  3. /** 
  4.  * 獲取文章的字數或則字符數 
  5.  * @author montao 
  6.  */ 
  7. public class StatWordCount { 
  8.      
  9.     private final char[] CHS = {',',';','!','.','!','?',';','+',',','?','!','/'};  //符號數組 
  10.      
  11.     private final char[] CHN = {'\r','\n'}; //轉義符數組 
  12.      
  13.     private final char[] SPACE = {' ',' '}; //空格的數組(前半角,後全角) 
  14.      
  15.     /** 
  16.      * 根據指定條件來篩選文章的字數 
  17.      * @param wordContent  文章內容 
  18.      * @param compriseInterpunction  是否包含指定字符 
  19.      * @param compriseSpace  是否包含空格 
  20.      * @return 返回文章經過指定篩選後的長度 
  21.      */ 
  22.     public int getWordCount(String wordContent,boolean compriseInterpunction,boolean compriseSpace) 
  23.     { 
  24.         if(wordContent==null){ 
  25.             return 0
  26.         }else if(wordContent.length()==0){ 
  27.             return 0;  
  28.         }else
  29.             //既要包含符號又要包含空格 
  30.             if(compriseInterpunction && compriseSpace){ 
  31.                 //清除轉義符 
  32.                 String regex = "["+new String(CHN)+"]"
  33.                 wordContent = wordContent.replaceAll(regex," "); 
  34.                 return this.getWordCount(wordContent); 
  35.             } 
  36.             //不包含符號包含空格 
  37.             else if(!compriseInterpunction && compriseSpace){ 
  38.                 //使用正則表達式去掉指定的符號和轉義符 
  39.                 String regex1 = "["+new String(CHN)+"]"
  40.                 String regex2 = "["+new String(CHS)+"]"
  41.                 wordContent = wordContent.replaceAll(regex1," "); 
  42.                 wordContent = wordContent.replaceAll(regex2," "); 
  43.                 return this.getWordCount(wordContent); 
  44.             } 
  45.             //包含指定符號不包含空格 
  46.             else if(compriseInterpunction && !compriseSpace){ 
  47.                 //使用正則表達式去掉空格和轉義符 
  48.                 String regex1 = "["+new String(CHN)+"]"
  49.                 String regex2 = "["+new String(SPACE)+"]"
  50.                 wordContent = wordContent.replaceAll(regex1," "); 
  51.                 wordContent = wordContent.replaceAll(regex2," "); 
  52.                 return this.getWordCount(wordContent); 
  53.             } 
  54.             //空格和指定符號都不包含 
  55.             else
  56.                 //使用正則表達式去掉空格,指定符號和轉義符 
  57.                 String regex1 = "["+new String(CHN)+"]"
  58.                 String regex3 = "["+new String(CHS)+"]"
  59.                 String regex2 = "["+new String(SPACE)+"]"
  60.                 wordContent = wordContent.replaceAll(regex1," "); 
  61.                 wordContent = wordContent.replaceAll(regex2," "); 
  62.                 wordContent = wordContent.replaceAll(regex3," "); 
  63.                 return this.getWordCount(wordContent); 
  64.             } 
  65.         } 
  66.     } 
  67.      
  68.     /** 
  69.      * 返回文章中的字數 
  70.      * @param wordCount 文章內容 
  71.      * @return 
  72.      */ 
  73.     @SuppressWarnings("unused"
  74.     private int getWordCount(String wordContent){ 
  75.         int count = 0
  76.         if(wordContent==null){ //判斷是否爲null,如果爲null直接返回0 
  77.             count = 0
  78.         }else if(wordContent.length()==0){ //判斷是否爲空,如果爲空直接返回0 
  79.             count = 0
  80.         }else//判斷獲取字數 
  81.             wordContent = wordContent.trim(); //清空空格 
  82.             //臨時變量 
  83.             String s4 = ""
  84.             String s3 = ""
  85.             String s2 = ""
  86.             String s1 = ""
  87.             boolean bb = false
  88.             if(wordContent.length()>0){ 
  89.                 s4 = String.valueOf(wordContent.charAt(wordContent.length()-1)); 
  90.             } 
  91.             for(int i=0;i<wordContent.length();i++){ 
  92.                 s3 = String.valueOf(wordContent.charAt(i)); 
  93.                 int num = s3.getBytes().length; 
  94.                 if(s3.hashCode()==32||s3.getBytes().length==2){ 
  95.                     bb=true
  96.                 }if(num==2){ 
  97.                     count++; 
  98.                 }else
  99.                         if(i+1<wordContent.length()){ 
  100.                             s1 = String.valueOf(wordContent.charAt(i+1)); 
  101.                             if((s1.hashCode()==32 && (s3.hashCode()!=32)) || ((s1.getBytes().length==2)&& (s3.hashCode()!=32))){ 
  102.                                 count++; 
  103.                             } 
  104.                         } 
  105.                 } 
  106.             } 
  107.             if(!bb){ 
  108.                 count++; 
  109.             }else
  110.                 if(s4.getBytes().length==1){ 
  111.                     count++; 
  112.                 } 
  113.             } 
  114.         } 
  115.         return count; 
  116.     } 
  117.      
  118.     /** 
  119.      * 根據條件來獲取文章的字符數 
  120.      * @param wordContent 文章內容 
  121.      * @param compriseInterpunction 是否包含指定符號 
  122.      * @param compriseSpace 是否包含空格 
  123.      * @return 返回字符長度 
  124.      */ 
  125.     public int getWordCharacter(String wordContent,boolean compriseInterpunction,boolean compriseSpace) 
  126.     { 
  127.         //既要包含符號又要包含空格 
  128.         if(compriseInterpunction && compriseSpace){ 
  129.             //清除轉義符 
  130.             String regex = "["+new String(CHN)+"]"
  131.             wordContent = wordContent.replaceAll(regex,""); 
  132.             //首部的空格不算 
  133.             wordContent = wordContent.replaceAll("^\\s+",""); 
  134.             return wordContent.length(); 
  135.         }//不包含符號包含空格 
  136.         else if(!compriseInterpunction && compriseSpace){ 
  137.             //首部的空格不算 
  138.             wordContent = wordContent.replaceAll("^\\s+",""); 
  139.             //使用正則表達式去掉指定的符號和轉義符 
  140.             String regex1 = "["+new String(CHN)+"]"
  141.             String regex2 = "["+new String(CHS)+"]"
  142.             wordContent = wordContent.replaceAll(regex1,""); 
  143.             wordContent = wordContent.replaceAll(regex2,""); 
  144.             return wordContent.length(); 
  145.         }//包含指定符號不包含空格 
  146.         else if(compriseInterpunction && !compriseSpace){ 
  147.             //使用正則表達式去掉空格和轉義符 
  148.             String regex = "["+new String(SPACE)+"]"
  149.             wordContent = wordContent.replaceAll(regex," "); 
  150.             return this.getNoSpaceCount(wordContent); 
  151.         }//空格和指定符號都不包含 
  152.         else
  153.             //使用正則表達式去掉指定符號 
  154.             String regex1 = "["+new String(CHS)+"]"
  155.             String regex2 = "["+new String(SPACE)+"]"
  156.             wordContent = wordContent.replaceAll(regex1," "); 
  157.             wordContent = wordContent.replaceAll(regex2," "); 
  158.             return this.getNoSpaceCount(wordContent); 
  159.         } 
  160.     } 
  161.  
  162.     /** 
  163.      * 獲取文章中非空格的字符總數 
  164.      * @param wordContent 文章內容 
  165.      * @return 
  166.      */ 
  167.     private int getNoSpaceCount(String wordContent) { 
  168.         int spaceCount = 0
  169.         if(wordContent==null
  170.         { 
  171.             spaceCount = 0
  172.         }else if(wordContent.length()==0
  173.         { 
  174.             spaceCount = 0
  175.         }else 
  176.         { 
  177.             //替換首部的 
  178.             wordContent = wordContent.replaceAll("^\\s+",""); 
  179.             wordContent = wordContent.replaceAll(" ",""); 
  180.             //使用正則替換轉義符 
  181.             String regex = "["+new String(CHN)+"]"
  182.             wordContent = wordContent.replaceAll(regex,""); 
  183.             spaceCount = wordContent.length(); 
  184.         } 
  185.         return spaceCount; 
  186.     } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章