JS how to determine whether the text is full-width or half-width (reproduced)
Reprinted from: http://www.php.cn/js-tutorial-362638.html
Full angle: is a computer character that refers to the position of a full-width character occupying two standard characters (or two half-width characters). The full angle occupies two bytes. Half-width: refers to a character occupying a standard character position. Half-width occupies one byte. Next through this article to introduce you to JS verification of full-width and half-angle and mutual transformation of knowledge, need to refer to the friend
1. What is full-width and half-width?
Full angle: is a computer character that refers to the position of a full-width character occupying two standard characters (or two half-width characters). The full angle occupies two bytes.
Chinese characters and the full-width of the English characters and the national standard gb2312-80 in the graphic symbols and special characters are full-width characters. In the full-width, letters and numbers, like Chinese characters, occupy the same wide position.
Half-width: refers to a character occupying a standard character position. Half-width occupies one byte.
The half-width is the ASCII character, and the letters, numbers, and characters are half-angled when no Chinese input method is in effect.
Each half-width character occupies only one byte of space (one byte has 8 bits, a total of 256 encoded spaces). Chinese, Japanese, and Korean font size of ideographic language is much larger than 256 encoding space, so instead of two bytes to store. At the same time, because of the writing habits of Chinese, Japanese and Korean hieroglyphics, if the uniform use of full-width characters, the arrangement also appears neat.
In order to arrange neatly, English and other Latin characters and punctuation are also available in full-width format.
2. The difference between full-width and half-width
Full-width and half-width are mainly for punctuation, full-width punctuation is two bytes, and half-width is one byte. Both the half-width and the full-width of the Chinese characters account for two bytes.
3.js determines whether the input text is full-width or half-width?
str="Chinese;;A"
alert(str.match(/[\u0000-\u00ff]/g)) //Half-width
alert(str.match(/[\u4e00-\u9fa5]/g)) //Chinese
alert(str.match(/[\uff00-\uffff]/g)) //full width
4.js Mutual transformation of full-angle and half-angle
First, the following information should be clarified:
A. Full-width space is 12288, half-width space is 32
B. Other character half-width (33-126) and full-width (65281-65374) correspondence is: the difference between 65248
Half angle converted to full angle
function ToDBC(txtstring) {
var tmp = "";
for(var i=0;i<txtstring.length;i++{
if(txtstring.charCodeAt(i)==32){
tmp= tmp+ String.fromCharCode(12288);
}
if(txtstring.charCodeAt(i)<127){
tmp=tmp+String.fromCharCode(txtstring.charCodeAt(i)+65248);
}
}
return tmp;
}
|
|
The above uses the JS charCodeAt () method and the fromCharCode () method.
The charCodeAt () method returns the Unicode encoding of the character at the specified position. This return value is an integer between 0-65535.
fromCharCode () can accept a specified Unicode value and then return a string.
To learn more about the charCodeAt () method and the fromCharCode () method, you can click the JavaScript charcodeat () method and the JavaScript fromCharCode () method.
Full-width conversion to half-width
function ToCDB(str) {
var tmp = "";
for(var i=0;i<str.length;i++){
if (str.charCodeAt(i) == 12288){
tmp += String.fromCharCode(str.charCodeAt(i)-12256);
continue;
}
if(str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375){
tmp += String.fromCharCode(str.charCodeAt(i)-65248);
}
else{
tmp += String.fromCharCode(str.charCodeAt(i));
}
}
return tmp
}
JS how to determine whether the text is full-width or half-width (reproduced)