判断中文半角全角的问题:

kevinhu 2005-02-23 01:36:35
判断中文半角全角的问题:
程序中有一文本,文本的内容只能输入半角,不能输入全角,必须进行判断?如何写代码?
...全文
237 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
starsoulxp 2005-02-23
  • 打赏
  • 举报
回复
参考http://java-cn.com/technology/technology_detail.jsp?id=2383


写一个toGB(String str)函数,如果toGB(String str)里面的str不能正常转化成
gb2312的字符串,
那么显示为空白,toGB(String str).length()为0
假如想返回前100个字,
这样就可以考虑用toGB(String.substring(0,200));
然后判断toGB().length()是否是0,如果是0,则substring(0,200-1);

<% //-------整个程序如下---------%>
<%!public static String toGB(String str){

try{
str=new String(str.getBytes("ISO8859_1"),("GB2312"));
return str;
}
catch(Exception e){
return null;
}
}

//-----------------------------------------------------------------

//.......
//和数据库的连接
//.......

String content;

content=rs.getString("content");

int Ccount;
if((Ccount=content.length())>200){
Ccount=200;
}
if(toGB(content.substring(0,Ccount)).length()==0){
content=content.substring(0,Ccount-1);
}else{
content=content.substring(0,Ccount);
}

out.print(toGB(content));
%>
xitianjile 2005-02-23
  • 打赏
  • 举报
回复
不知道不知道。

把字符串转成char数组。
根据每个char的值的范围判断。
starsoulxp 2005-02-23
  • 打赏
  • 举报
回复
转换程序(javascript)
/****************************
* 参数说明:
* 全角->半角
* str:要转换的字符串
* 返回值类型:字符串
**************************** /
function DBC2SBC(str) {
var i;
var result='';
for(i=0;i<str.length;i++) {
code=str.charCodeAt(i);
// “65281”是“!”,“65373”是“}”
if(code>=65281&&code<65373)
// “65248”是转换码距
result+=String.fromCharCode(str.charCodeAt(i)-65248);
else result+=str.charAt(i);
}
alert(result);
return result;
}
/*****************************/
以上程序能转换以下编码。若要转换其他特殊的全角符号,请参考http://ha1.seikyou.ne.jp/home/akairingosaita/hangul/dec-unicode-ks.txt
65281 !
65282 "
65283 #
65284 ¥
65285 %
65286 &
65287 '
65288 (
65289 )
65290 *
65291 +
65292 ,
65293 -
65294 .
65295 /
65296 0
65297 1
65298 2
65299 3
65300 4
65301 5
65302 6
65303 7
65304 8
65305 9
65306 :
65307 ;
65308 <
65309 =
65310 >
65311 ?
65312 @
65313 A
65314

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧