比word先进的字数统计
来源:互联网 发布:蛇 知乎 编辑:程序博客网 时间:2024/05/16 18:47
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;
}
}
文章来源自:http://topic.csdn.net/u/20080424/04/279b07e4-a5a2-4af6-a285-6081db222d62.html
- 比word先进的字数统计
- Interop统计WORD字数
- 不启动Word查看文档字数统计的方法
- 汉字字数的统计
- .NET实现ms-Word中字数统计
- vba 统计Word 字数、页数等信息
- python统计文本文件的字数
- 一个统计字数的程序
- 带有字数统计的 TextInputLayout
- 一个统计字数的程序
- Word字数统计怎么用?2003/2007/2010统计字数全攻略!
- 字数统计
- 字数统计
- 字数统计
- 将Doc或者Docx文档处理成html的代码逻辑;统计word中的字数,段数,句数,读取word中文档内容的代码逻辑
- php统计中英文混合的文章字数
- jQuery实现统计字数的功能模块
- 一个jquery统计字数的小功能
- PowerDesigner UML 建模
- FCKeditor在Java环境下的使用方法
- 生活杂谈:“熟能生巧”何为“生”?
- 昨天晚上的培训~真的是好事多磨啊!
- jsp:include与include的区别
- 比word先进的字数统计
- 巧用 mutable
- 谁主沉浮 OpenCL与CUDA架构深入解析
- MyEclipse 7.0 注册码
- 判断页面文本框中输入的类型(数字,符号,字母,汉字)!!
- jsp:include动作和include指令的区别
- uC/OS-II源码分析(一)
- 如何在命令行删除在会话设置的启动程序
- 在使用SSIS设计脚本时候,出现的在PostExecute之外不能锁定变量集合进行读写访问的问题的解决