C # finds all positions of a character in a string

Source: Internet
Author: User
Tags tostring trim

When you find all the positions of a character in a string, you can first convert the string to an array of type char by using the ToCharArray method, and then iterate through the array, removing its position if the value in the array is the same as the character you are looking for. Find the implementation code for a character in all positions in the string as follows:

private void Button1_Click (object sender, EventArgs e)
{
String str = TextBox1.Text.Trim ();
char[] MyChar = str. ToCharArray ();
for (int i = 0; i < mychar.length; i++)
{
if (mychar[i). ToString () = = TextBox2.Text.Trim ())
MessageBox.Show ("string" + TextBox2.Text.Trim () + "in" + TextBox1.Text.Trim () + "position is:" + i.tostring () + "\ n");
}
}

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.