使用indexOf替换char的出现次数

时间:2014-06-05 14:53:20

标签: c# replace indexof

如何使用indexOf替换多个输入字母?我现在可以找到所有索引出现,如果它们以-1结尾。转换为char时,我也得到了一个无法处理的异常。

static Random rnd = new Random();
    static void Main(string[] args)
    {
        string wrong = "";

        ArrayList list = new ArrayList();
        list.Add("Dune");
        list.Add("The Lord of the Rings");
        list.Add("The Iliad");
        list.Add("Hamlet");

        int r = rnd.Next(list.Count);
        Console.WriteLine((string)list[r]);
        string s = (string)list[r];

        string patten = "[a-zA-Z0-9]";
        Regex rgx = new Regex(patten);

        string sDash = rgx.Replace(s, "-");

        do
        {
            string a = " ";

            Console.Write("Guess a letter: ");
            a = Console.ReadLine();

            while (a.Length != 1)
            {
                Console.WriteLine("Please enter only one letter:");
                a = Console.ReadLine();
            }

            char myChar = a[0];
            int input = s.IndexOf(a);
            while (input != -1)
            {
                Console.WriteLine(input);
                input = s.IndexOf(myChar, input + 1);
            }

            if (input != -1)
            {
                StringBuilder builder = new StringBuilder(sDash);
                builder[input] = myChar;

                sDash = builder.ToString();
                Console.WriteLine(sDash + String.Compare(sDash, s, true));
            }
            else {
                StringBuilder builder = new StringBuilder(wrong);
                builder.Append(a);
                string wrongAnswers = builder.ToString();
                Console.WriteLine("Wrong, try again. \n {0}", wrongAnswers);
            }
        } while (String.Compare(sDash, s) == -1);

    }

1 个答案:

答案 0 :(得分:0)

我会这样做:

static Random rnd = new Random();
static void Main(string[] args)
{
    string wrong = "";

    List<String> list = new List<String>();
    list.Add("Dune");
    list.Add("The Lord of the Rings");
    list.Add("The Iliad");
    list.Add("Hamlet");

    int r = rnd.Next(list.Count);
    Console.WriteLine((string)list[r]);
    string s = (string)list[r];

    string patten = "[a-zA-Z0-9]";
    Regex rgx = new Regex(patten);

    string sDash = rgx.Replace(s, "-");
    StringBuilder wrongAnswers = new StringBuilder();
    do
    {
        string a = " ";

        Console.Write("Guess a letter: ");
        a = Console.ReadLine();

        while (a.Length != 1)
        {
            Console.WriteLine("Please enter only one letter:");
            a = Console.ReadLine();
        }

        char myChar = a.ToLower()[0];
        int input = s.ToLower().IndexOf(myChar);
        bool foundSomething = false;
        StringBuilder builder = new StringBuilder(sDash);
        while(input != -1)
        {
            foundSomething = true;
            builder[input] = myChar;
            try
            {
                input = s.ToLower().IndexOf(myChar, input + 1);
            }
            catch (ArgumentOutOfRangeException)
            {
                input = -1;
            }
        }
        sDash = builder.ToString();

        if (foundSomething)
        {
            Console.WriteLine(sDash + " " + sDash.ToLower().Equals(s.ToLower()));
        }
        else
        {
            wrongAnswers.Append(myChar);
            Console.WriteLine("Wrong, try again. \n {0}", wrongAnswers);
        }
    } while (!sDash.ToLower().Equals(s.ToLower()));

}

顺便说一下,这里可以做很多优化,但原则就在这里..