java实现:
/**
* 把字符串中的全角字符串转换成半角字符串
* 如果仅有半角字符串,则不变
* @param str
* @return
*/
public static String Q2BChange(String str){
String result = "";
int code = 0;
String inputStr = str.replaceAll("^[ | ]*","").replaceAll("[ | ]$","").replaceAll("[ | ]","");
for(int i=0;i<inputStr.length();i++){
code = inputStr.codePointAt(i);
System.out.println(code);
if(code>=65281 && code<65373){
//“65248”是转换码距
result += new String(new int[]{ code-65248 },0,1);
} else {
result += inputStr.charAt(i);
}
}
return result;
}
js实现(这个在网上找来的,但是那个里面有太多错误):
function DBC2SBC(str) {
var i;
var result='';
for(i=0;i<str.length;i++){
// “65281”是“!”,“65373”是“}”
code=str.charCodeAt(i);
if(code>=65281 && code<65373)
// “65248”是转换码距
result += String.fromCharCode( str.charCodeAt(i)-65248 );
else
result += str.charAt(i);
}
return result;
}
/**
* 把字符串中的全角字符串转换成半角字符串
* 如果仅有半角字符串,则不变
* @param str
* @return
*/
public static String Q2BChange(String str){
String result = "";
int code = 0;
String inputStr = str.replaceAll("^[ | ]*","").replaceAll("[ | ]$","").replaceAll("[ | ]","");
for(int i=0;i<inputStr.length();i++){
code = inputStr.codePointAt(i);
System.out.println(code);
if(code>=65281 && code<65373){
//“65248”是转换码距
result += new String(new int[]{ code-65248 },0,1);
} else {
result += inputStr.charAt(i);
}
}
return result;
}
js实现(这个在网上找来的,但是那个里面有太多错误):
function DBC2SBC(str) {
var i;
var result='';
for(i=0;i<str.length;i++){
// “65281”是“!”,“65373”是“}”
code=str.charCodeAt(i);
if(code>=65281 && code<65373)
// “65248”是转换码距
result += String.fromCharCode( str.charCodeAt(i)-65248 );
else
result += str.charAt(i);
}
return result;
}