JS how to determine whether the text is full-width or half-width (reproduced)

Source: Internet
Author: User



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)


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.