转换程序(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
/****************************
* 参数说明:
* 全角->半角
* 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
根据每个char的值的范围判断。
写一个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));
%>