Full-width: It is a kind of computer character, which means that one full-width character occupies the position of two standard characters (or two half-width characters). Full width occupies two bytes. Half-width: refers to a character occupying a standard character position. Half-width occupies one byte. Next, this article will introduce you to the knowledge of JS verification of full-width and half-width and mutual conversion. Friends who need it can refer to it
1. What are full-width and half-width?
Full-width: It is a kind of computer character, which means that one full-width character occupies the position of two standard characters (or two half-width characters). Full width occupies two bytes.
Chinese characters, English characters that specify full-width, and graphic symbols in the national standard GB2312-80 and special characters are all full-width characters. In full-width, letters and numbers occupy equal-width positions like Chinese characters.
Half-width: refers to a character occupying a standard character position. Half-width occupies one byte.
Half-width characters are ASCII characters. When no Chinese character input method works, the letters, numbers and characters entered are all half-width characters.
Each half-width character only occupies one byte of space (one byte has 8 bits, a total of 256 encoding spaces). The font size of hieroglyphic languages such as Chinese, Japanese, and Korean is much larger than 256 encoding spaces, so two bytes are used for storage. At the same time, due to the writing habits of hieroglyphics such as China, Japan, and Korea, if full-width characters are used uniformly, the arrangement will appear neat.
In order to arrange neatly, English and other Latin characters and punctuation are also provided in full-width format.
2. The difference between full-width and half-width
Full-width and half-width are mainly for punctuation marks. Full-width punctuation occupies two bytes. , half-width occupies one byte. Regardless of whether it is half-width or full-width, Chinese characters take up two bytes.
3.js determine whether the entered text is full-width or half-width?
str="中文;;a" alert(str.match(/[\u0000-\u00ff]/g)) //半角 alert(str.match(/[\u4e00-\u9fa5]/g)) //中文 alert(str.match(/[\uff00-\uffff]/g)) //全角
4.js mutual conversion of full-width and half-width
First of all, the following information must be clarified:
a. A full-width space is 12288, and a half-width space is 32
b. The corresponding relationship between half-width (33-126) and full-width (65281-65374) for other characters is: the difference is 65248
Convert half-width to full-width
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 charCodeAt() method and fromCharCode() method of js are used above.
The charCodeAt() method returns the Unicode encoding of the character at the specified position. This return value is an integer between 0 - 65535.
fromCharCode() accepts a specified Unicode value and returns a string.
If you want to know more about the charCodeAt() method and fromCharCode() method, you can click "JavaScript charCodeAt() Method" and "JavaScript fromCharCode() Method".
Convert full-width 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 }
[Related recommendations]
1. Javascript Free Video Tutorial
2. nodejs+websocket completes a chat system function
3. Js completes the countdown time difference effect
4. Detailed explanation of the method of recursively deleting elements in an array in JS
##5.Detailed explanation of an example of JS completing the star rating function
The above is the detailed content of How to determine whether text is full-width or half-width in JS. For more information, please follow other related articles on the PHP Chinese website!