[C#] C#检索出一个字符串中某字符第n次出现的位置(IndexOf) →→→→→进入此内容的聊天室

来自 , 2020-05-27, 写在 C#, 查看 107 次.
URL http://www.code666.cn/view/0d8f8313
  1.  class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             Console.WriteLine("Please input your STRING:");
  6.             string a = Console.ReadLine();
  7.             char c;
  8.             char find_c;
  9.             Console.WriteLine("Please input the CHARACTER in your string:");
  10.             find_c = Convert.ToChar(Console.ReadLine());
  11.             int count = 0;
  12.             for (int i = 0; i < a.Length; i++)
  13.             {
  14.                 c = a[i];
  15.                 if (c == find_c)//求得a中包含该字符的个数,以便遍历
  16.                 {
  17.                     count++;
  18.                 }
  19.             }
  20.             Console.WriteLine("There are total {0} of char '{1}' in your input string.",count,find_c);
  21.             int index = 0;
  22.             int n;//第n个find_c
  23.             Console.WriteLine("Please input the SEQUENCE of the char '{0}' in your input string:", find_c);
  24.             n = Convert.ToInt32(Console.ReadLine());
  25.             if (n > count)
  26.             {
  27.                 Console.WriteLine("Error:The Num must be less than or equal to {0}.",count);
  28.                 Console.ReadKey();
  29.                 return;
  30.             }
  31.             for (int j = 1; j <= count; j++)
  32.             {
  33.                 index = a.IndexOf(find_c,index);
  34.                 if (j == n)
  35.                 {
  36.                     break;
  37.                 }
  38.                 else
  39.                 {
  40.                     index = a.IndexOf(find_c,index+1);
  41.                 }
  42.  
  43.             }
  44.             Console.WriteLine("The Index of the No.{0} char '{1}' in your input string is {2}.", n, find_c, index);
  45.             Console.ReadKey();
  46.         }
  47.     }
  48. //csharp/6574

回复 "C#检索出一个字符串中某字符第n次出现的位置(IndexOf)"

这儿你可以回复上面这条便签

captcha