How to obtain the number of Chinese characters in a string in C # In asp.net

Source: Internet
Author: User

A string can contain numbers, letters, Chinese characters, or other characters. The IsDigit static method of the Char type can be used to determine whether the character in the string is a number, And the IsLetter static method of the Char type can be used to determine whether the character in the string is a letter. You can use this method to determine whether a string is a Chinese character. You can use this method to calculate the number of Chinese characters in the string.

First, based on the page and content of the Form, Box1.Text is the input string. We can process the string to calculate the number of Chinese characters, double-click the Buton control, and edit its Click Event code.

Let's take a look at the Unicode range of Chinese characters. Generally, 0x4E00 to 0x9FA5 is given, as long as the hexadecimal encoding of the characters we receive is in this range, we assume that this character is a Chinese character (the number of Chinese characters displayed in label4.Text)

Copy codeThe Code is as follows:
String a = Box1.Text;
Int K = 0;
For (int I = 0; I <a. Length; I ++)
{
Char j = a [I];
Ushort s = j;
If (s> = 0x4E00 & s <= 0x9FA5)
{
K ++;
}
Label4.Text = "" + K;

Then I sorted out some functions C # And used regular expressions to determine Chinese characters from strings.

Copy codeThe Code is as follows:
// Reference the namespace first
Using System. Text. RegularExpressions;

// Define a function to return the number of Chinese characters in the string
Public static int GetHanNumFromString (string str)
{
Int count = 0;
Regex regex = new Regex (@ "^ [u4E00-u9FA5] {0,} $ ");

For (int I = 0; I <str. Length; I ++)
{
If (regex. IsMatch (str [I]. ToString ()))
{
Count ++;
}
}

Return count;
}

Code Description:

1. Unicode characters u4E00 to u9FA5 are encoded in Chinese characters, so they are used to represent the regular expression matching range of Chinese characters.
2. the string can be indexed directly. The data type of the index value is char, so str [I]. ToString () is used to convert char to a string again.
3. Use the IsMatch method of Regex to determine whether the string matches the given regular expression. If the match succeeds, True is returned. Otherwise, False is returned.

For example, obtain the number of Chinese characters in a string C # obtain the number of full-width strings
Copy codeThe Code is as follows:
Static void Main (string [] args)
{
While (true)
{

Console. WriteLine ("enter a string of characters ");
String str = Console. ReadLine (); // defines a variable. str is used to store the input string.
Int num = Encoding. Default. GetByteCount (str); // Encoding. Default. GetByteCount (str) calculate the number of bytes produced by Encoding the characters by Default.
Int len = str. Length; // obtain the Length of a string.
Console. WriteLine ("{0} Chinese characters", num-len); // num-len indicates the number of Chinese characters in the string.
}
}

For example, this is also normal.

Copy codeThe Code is as follows:
ArrayList itemList = new ArrayList ();
CharEnumerator CEnumerator = textBox1.Text. GetEnumerator ();
Regex regex = new Regex ("^ [/u4E00-/u9FA5] {0,} $ ");
While (CEnumerator. MoveNext ())
{
If (regex. IsMatch (CEnumerator. Current. ToString (), 0 ))
ItemList. Add (CEnumerator. Current. ToString ());
TextBox2.Text = itemList. Count. ToString ();
}

The principle is: to obtain the number of Chinese characters in a string, you can first define a regular expression that matches the Chinese character, and then use the MoveNext method of the CharEnumerator object to cyclically access each character in the string, if the accessed character matches the defined regular expression, add it to an array and obtain the number of items in the array, that is, the number of Chinese characters in the string. Returns the number of Chinese characters in a string.

Related Article

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.