Insert variable values in the middle of a string

后端 未结 6 1982
生来不讨喜
生来不讨喜 2020-12-05 03:56

In C#: If I want to create a message like this: \"Hi We have these flights for you: Flight A,B,C,D. Which one do you want\"

where just the section i

6条回答
  •  难免孤独
    2020-12-05 04:57

    String.Format("Hi We have these flights for you: {0}. Which one do you want",
                                  flights);
    

    EDIT: you can even save the "template" string separately (for instance you could store it in a configuration file and retrieve it from there), like this:

    string flights = "Flight A, B,C,D";
    
    string template = @"Hi We have these flights for you: {0}. Which one do you want";
    Console.WriteLine(String.Format(template, flights));
    

    EDIT2: whoops, sorry I see that @DanPuzey had already suggested something very similar to my EDIT (but somewhat better)

提交回复
热议问题