asp.net C # To obtain the number of Chinese characters in the string of concrete implementation methods _ practical skills

Source: Internet
Author: User

A string can include numbers, letters, Chinese characters, or other characters. IsDigit static methods that use the char type can determine whether a character in a string is a number, and use the Isletter static method in the char type to determine whether the string is a letter. We will implement a method to determine whether the string is Chinese characters, through this method can calculate the number of characters in the string, the effect of running as shown:

First, according to the effect map set the interface and content of the form, Box1.text for the input string, we handle the string, to calculate the number of Chinese characters, double-click the Buton control, edit its click event code.

We look at the Unicode range of Chinese characters, generally given 0x4e00 to 0x9fa5, as long as we accept the character of the 16 code in this range, we think this character is kanji (label4. Text displays the number of Chinese characters)

Copy Code code 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;

And then I sorted some of the functions in C # use regular expressions to judge Chinese characters from strings

Copy Code code as follows:

Reference Namespaces First
Using System.Text.RegularExpressions;

Defines a function that returns the number of characters in a 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 character u4e00 to u9fa5 between the encoding, so use it to represent the regular matching range of Chinese characters.
2, the string can be indexed directly, the index of the value of the data type is char, so str[i]. ToString () is to convert char to a string again.
3. Using the Regex IsMatch method, you can determine whether the string matches a given regular expression, or False if the match succeeds.

example, gets the number of Chinese characters in a string C # gets the number of full-width strings

Copy Code code as follows:

static void Main (string[] args)
{
while (true)
{

Console.WriteLine ("Enter a string of characters");
String str = Console.ReadLine ();//define a variable str to store the input string
int num= Encoding.Default.GetByteCount (str);//encoding.default.getbytecount (str) calculates the number of bytes produced by default on the system
int len = str. length;//gets the length of the string
Console.WriteLine ("{0} Chinese characters", Num-len);//num-len is the number of characters in a string
}
}

For example, this is normal.

Copy Code code 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 get the number of Chinese characters in a string, you can first define a regular expression that matches the Chinese character, and then iterate through each character in the string using the MoveNext method of the Charenumerator object, if the character being accessed matches the defined regular expression, is added to an array, the number of characters in the string is obtained by the end of the array. Get the number of 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.