[csharp] class Program { static void Main(string[] args) { Console.WriteLine("Please input your STRING:"); string a = Console.ReadLine(); char c; char find_c; Console.WriteLine("Please input the CHARACTER in your string:"); find_c = Convert.ToChar(Console.ReadLine()); int count = 0; www.2cto.com for (int i = 0; i < a.Length; i++) { c = a[i]; if (c == find_c)//求得a中包含該字符的個數,以便遍歷 { count++; } } Console.WriteLine("There are total {0} of char '{1}' in your input string.",count,find_c); int index = 0; int n;//第n個find_c Console.WriteLine("Please input the SEQUENCE of the char '{0}' in your input string:", find_c); n = Convert.ToInt32(Console.ReadLine()); if (n > count) { Console.WriteLine("Error:The Num must be less than or equal to {0}.",count); Console.ReadKey(); return; } for (int j = 1; j <= count; j++) { index = a.IndexOf(find_c,index); if (j == n) { break; } else { index = a.IndexOf(find_c,index+1); } } Console.WriteLine("The Index of the No.{0} char '{1}' in your input string is {2}.", n, find_c, index); Console.ReadKey(); } }