Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:请问如何计算一段文字里面包含多少个中文字符? [Re:surroad]
ratking





发贴: 2
积分: 0
于 2006-07-21 10:38 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
/*
* TestGbk.java
*
* Copyleft (C) 2006 RatKing. All wrongs reserved.
*/

import java.io.UnsupportedEncodingException;

/**
* 测试GBK汉字.
*
* 用法举例:
* java TestGbk "我是谁?Who am I?"
*
* @author <a href="mailto:ratking@ynet·com">RatKing</a>
* @version 1.0 (build 20060721)
*
* @see <a href="http://www.haiyan.com/steelk/navigator/ref/gbk/gbindex.htm">《GBK 汉字内码扩展规范》</a>
*/
public class TestGbk {
/** GBK编码高字节最小值 */
public static final int GBK_HIGH_MIN = 0x81;
/** GBK编码高字节最大值 */
public static final int GBK_HIGH_MAX = 0xFE;
/** GBK编码低字节最小值 */
public static final int GBK_LOW_MIN = 0x40;
/** GBK编码低字节最大值 */
public static final int GBK_LOW_MAX = 0xFE;
/** GBK编码低字节例外值 */
public static final int GBK_LOW_EXCEPTION = 0x7F;

public static void main(String[] args) {
String str = null;
if (args == null || args.length == 0) {
str = "Hello, world!世界,你好!";
} else if (args.length != 1
|| (args.length == 1
&& (args[0].equals("/?")
|| args[0].equals("-h")
|| args[0].equals("-help")
|| args[0].equals("--help")))) {
System.err.println("用法:java TestGbk [字符串]");
System.exit(1);
} else {
str = args[0];
}

int gbkCount = 0;
int asciiCount = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
boolean isGbk = isGbk(c);
System.out.println(i + ":[" + c + "] " + isGbk);
if (isGbk) {
gbkCount++;
} else {
asciiCount++;
}
}
System.out.println("字符串:\"" + str + "\"");
System.out.println("GBK汉字(含全角标点符号)数:" + gbkCount);
System.out.println("非GBK汉字数:" + asciiCount);
System.out.println("字符串总字符数:" + str.length());
System.out.println("字符串总字节数:" + (gbkCount * 2 + asciiCount));
}

/**
* 是否为GBK内码的字符.
* 对英文字母、阿拉伯数字、半角标点符号,返回false;
* 对中文汉字、全角标点符号,返回true。
* @param c 待检测的字符
* @return 是否为GBK内码的字符
*/
public static boolean isGbk(char c) {
String str = String.valueOf(c);
byte[] b = null;
try {
b = str.getBytes("GBK");
} catch (UnsupportedEncodingException uee) {
throw new IllegalStateException("内部错误:本计算机的Java环境不支持GBK编码: "
+ uee.getMessage());
}
if (b.length == 2) {
int ch0 = b[0]; // 高位字节
int ch1 = b[1]; // 低位字节
if (ch0 < 0) {
ch0 += 256;
}
if (ch1 < 0) {
ch1 += 256;
}
if (ch0 >= GBK_HIGH_MIN && ch0 <= GBK_HIGH_MAX
&& ch1 >= GBK_LOW_MIN && ch1 <= GBK_LOW_MAX
&& ch1 != GBK_LOW_EXCEPTION) {
return true;
}
}

return false;
}
}



TestGbk.java (3.19k)


ratking edited on 2006-07-21 10:45


话题树型展开
人气 标题 作者 字数 发贴时间
8433 请问如何计算一段文字里面包含多少个中文字符? surroad 24 2006-07-16 14:39
7182 Re:请问如何计算一段文字里面包含多少个中文字符? myohmy 9 2006-07-16 18:15
7315 Re:请问如何计算一段文字里面包含多少个中文字符? 开心傻瓜 151 2006-07-16 22:01
7303 Re:请问如何计算一段文字里面包含多少个中文字符? cxp108 85 2006-07-17 08:35
7214 Re:请问如何计算一段文字里面包含多少个中文字符? myohmy 57 2006-07-17 20:06
7391 Re:请问如何计算一段文字里面包含多少个中文字符? ynwso 15 2006-07-19 13:32
7258 Re:请问如何计算一段文字里面包含多少个中文字符? cxp108 35 2006-07-20 11:44
7204 Re:请问如何计算一段文字里面包含多少个中文字符? 独孤酒间 162 2006-07-21 10:18
7861 Re:请问如何计算一段文字里面包含多少个中文字符? ratking 3080 2006-07-21 10:38

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923