C # Gets the length of a number or letter in a string

Source: Internet
Author: User

When you get the length of numbers or letters in a string in your program, you can iterate through each character in the string using the MoveNext method of the Charenumerator object. The characters are encoded using the ASCII-encoded GetBytes method in the System.Text.Encoding class, and then determine whether the ASCII value of the encoded character is between the specified values and, if so, add it to an array, and finally the number of items in the array is obtained. The key code to get the number or letter length in a string is as follows:

ArrayList itemList = new ArrayList ();
Charenumerator CEnumerator = TextBox1.Text.GetEnumerator ();
while (Cenumerator.movenext ())
{
byte[] array = new BYTE[1];
Array = System.Text.Encoding.ASCII.GetBytes (CEnumerator.Current.ToString ());
int asciicode = (short) (array[0]);
if ((Asciicode >= && asciicode <= 57) | | (Asciicode >= && asciicode <= 90) | | (Asciicode >= && asciicode <= 122))
{
Itemlist.add (CEnumerator.Current.ToString ());
}
TextBox2.Text = ItemList.Count.ToString ();
}

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.