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)
The code is as follows |
Copy Code |
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
code is as follows |
copy code |
//reference namespace first using System.Text.RegularExpressions; //Define a function that returns 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 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
The code is as follows |
Copy Code |
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.
code is as follows |
copy code |
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: When you 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 you are accessing 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. Gets the number of characters in the string