C# get position of a character from string

后端 未结 2 2017
轻奢々
轻奢々 2021-01-29 09:37

My code is this:

 string dex = \"ABCD1234\";
 string ch = \"C\";
 string ch1, ch2;
    if (dex.Contains(ch))
    {
       string n = Convert.ToChar(dex);
                


        
2条回答
  •  死守一世寂寞
    2021-01-29 10:14

    string aS = "ABCDEFGHI";
    char ch = 'C';
    int idx = aS.IndexOf(ch);
    MessageBox.Show(string.Format("{0} is in position {1} and between {2} and {3}", ch.ToString(), idx + 1, aS[idx - 1], aS[idx + 1]));
    

    This wont handle if your character is at position zero and some other conditions, you'll have to figure them out.

提交回复
热议问题