Read the length of the Chinese content contained in the C # Textbox Control

Source: Internet
Author: User
When we use the Textbox Control to add content to the database Program The reliability of the input content in textbox must be limited to fit the length defined in the database. The Textbox Control has a maxlength attribute, which can limit the maximum length of input characters in textbox, this attribute can help us limit the input length. However, if the input contains a mix of Chinese and English characters, this attribute is not perfect because it does not treat a Chinese character as two characters, therefore, this will usually cause a little trouble for some friends who have just come into contact with the Textbox Control Programming. To solve this problem, let's take a brief analysis and then provide a solution example, hoping to inspire others.
The text in the textbox control is of the string type and is of the Unicode encoding type. Therefore, I only need to obtain the Unicode value range of Chinese characters and analyze the strings one by one, if 2 is added to the range of Chinese Unicode values, otherwise 1 is added. In this way, the actual length of the textbox containing the Chinese and English content is obtained.
Note: The first 128 Unicode characters (16 characters Code From 0x0000 to 0x007f) is an ASCII character. The following 128 Unicode characters (code from 0x0080 to 0x00ff) are ASCII extensions of ISO 8859-1.
* Hieroglyphics (CJK) in China, Japan, and South Korea occupy code from 0x3000 to 0x9fff.
* The Greek alphabet uses code from 0x0370 to 0x03ff
* Use the code from 0x0400 to 0x04ff In the Slavic language
* Code from 0x0530 to 0x058f is used in the United States.
* Use the code from 0x0590 to 0x05ff in Hebrew.

Simple code implementation:

1 Public   Static   Int Gettextboxlength ( String Textboxtextstr)
2 {
3 Int Nlength =   0 ;
4 For ( Int I =   0 ; I < Textboxtextstr. length; I ++ )
5 {
6 If (Textboxtextstr [I] > =   Zero x 3000   && Textboxtextstr [I] <=   0x9fff )
7 Nlength + =   2 ;
8 Else
9 Nlength ++ ;
10 }
11 Return Nlength;
12 }

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.