C# String中字符查找输出问题

来源:百度知道 编辑:UC知道 时间:2024/06/06 18:32:35
namespace ConsoleApplication31
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Input a string!");
string s = Console.ReadLine();
char x = s[0];
for (int i = 0; i < s.Length; i++)
{
do
{
Console.Write("No.{0} char is {1}", i+1,s[i]);
Console.WriteLine("\n");
}
while (x == 'a');
}
}
}
}
我想实现的是用户输入string s,一开始是想做一个s的遍历;
后来又想查询s中所有的a的位置。
不晓得怎么写了,新手,麻烦高手解答

int i =0;
while(i<s.Length)
{
if (s[i]=='a') Console.WriteLine(i.ToString());
i++;
}

namespace ConsoleApplication31
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Input a string!");
string s = Console.ReadLine();
// char x = s[0];
for (int i = 0; i < s.Length; i++)
{
if(s[i]=='a')
{
Console.Write("No.{0} char is \"a\"", i + 1);
Console.WriteLine("\n");
}
}
}
}
}

如上