如何在文本中添加+字符串?

时间:2017-11-21 15:29:40

标签: string readline console.writeline

我想知道如何在Console.WriteLine / Console.Write中添加[+ line]。

例如:

string line;
    Console.WriteLine("Enter one or more lines of text (press 'gg' to exit):");
    do
    {
       Console.Write(" ");
        line = Console.ReadLine();
        if (line != "b")
            Console.WriteLine(" Sorry, you wrote **???**, which is wrong. \n Try again." +line);
       else
           Console.WriteLine("u rock");
    }
           while ( line != "gg") ;

我想在句子中间加上[+ line],问号是。 我该怎么做?

1 个答案:

答案 0 :(得分:0)

String interpolation是你最好的选择(假设你是在C#6或以上)。记住字符串开头的$:

Console.WriteLine($"Sorry, you wrote {line}, which is wrong. \n Try again.");

你也可以用来分隔加在一起的字符串,虽然这有点笨重:

Console.WriteLine("Sorry, you wrote " +  line + ", which is wrong. \n Try again.");