根据位置C#

时间:2016-03-29 04:47:37

标签: c# regex string replace stringbuilder

我有一个不完整的乐队名称列表,例如

string band1 = "ONE ..."string band2 = "... 5"string band3 = "30 ... ... ..."string band4 = "The ... Stones"

我需要替换字符...以形成完整乐队的名称,以便它们成为

ONE DIRECTIONMAROON 530 SECONDS TO MARSTHE ROLLING STONES

我有相关的答案,例如可以与DIRECTION结合以形成string band1 = ONE ...的字符串ONE DIRECTION。我的问题是,因为...字符可能位于字符串ONE之前或之后,我如何确保创建ONE DIRECTION而不是DIRECTION ONE等等?

3 个答案:

答案 0 :(得分:0)

使用string.StartsWith,string.EndsWith和string.Length的组合来确定正确的替换

答案 1 :(得分:0)

使用示例字符串尝试此示例。你会明白的。

static void Main(string[] args)
        {
            string question = string.Empty;
            string answer = string.Empty;
            string formattedString = string.Empty;
            question = Console.ReadLine();
            Console.WriteLine("Replace ... with:");
            answer = Console.ReadLine();
            formattedString = question.Replace("... ", answer);
            Console.WriteLine(formattedString);
            Console.ReadLine();
        }

答案 2 :(得分:0)

编辑2:

在此解决方案中,我将您的字符串...拆分为字符串数组。像这样,你知道...放在哪里,无论它们有多少,它们在哪里,最后合并它们。

此外,我正在使用Dictionary,这是动态的。

看看代码:

static void Main(string[] args)
{
    Dictionary<string, string> bands = new Dictionary<string, string>();
    bands.Add("30 ... ... ...", "SECONDS TO MARS");
    bands.Add("... 5", "MAROON");
    bands.Add("... STEPS ... ...", "TWO FROM HELL");

    foreach (KeyValuePair<string, string> band in bands)
    {
        bool solved = false;

        while (!solved)
        {
            Console.WriteLine("current band: " + band.Key);

            string input = Console.ReadLine();

            if (band.Value == input.ToUpper())
            {
                Console.WriteLine("correct");
                string[] splittedQuestion = band.Key.Split(new string[] { "..." }, StringSplitOptions.None);
                string[] splittedAnswer = band.Value.Split(' ');

                // fill splittedQuestion string with answer values
                for (int i = 0; i < splittedAnswer.Count(); i++)
                {
                    int currentIndex = GetNextDotIndex(splittedQuestion);
                    if (currentIndex != -1)
                    {
                        splittedQuestion[currentIndex] = splittedAnswer[i];
                    }
                }

                // build result
                string result = "";
                for (int i = 0; i < splittedQuestion.Count(); i++)
                {
                    result += splittedQuestion[i].Trim().ToUpper();
                    if (i < splittedQuestion.Count() - 1)
                    {
                        result += " ";
                    }
                }

                Console.WriteLine(result);
                solved = true;
            }
            else
            {
                Console.WriteLine("wrong");
            }
        }
    }
    Console.WriteLine("finished");
    Console.ReadLine();
}

private static int GetNextDotIndex(string[] splittedQuestion)
{
    for (int j = 0; j < splittedQuestion.Count(); j++)
    {
        if (splittedQuestion[j] == "" || splittedQuestion[j] == " ")
        {
            return j;
        }
    }
    // return -1, when no more ... are available
    return -1;
}