日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java字数统计_java统计字数

發布時間:2025/3/15 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java字数统计_java统计字数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/**

* 獲取文章的字數或則字符數

* @author montao

*/

public class StatWordCount {

private final char[] CHS = {',',';','.','!','?',';','+','。','?','!'};? //符號數組

private final char[] CHN = {'\n','\r'}; //轉義符數組

//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;is3 = 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+11)){

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;

}

}

閱讀(1947) | 評論(0) | 轉發(0) |

總結

以上是生活随笔為你收集整理的java字数统计_java统计字数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。