Insert variable values in the middle of a string

后端 未结 6 1981
生来不讨喜
生来不讨喜 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:41

    You can use string.Format:

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

    You should load template from your resource file and data is your runtime values.

    Be careful if you're translating to multiple languages, though: in some cases, you'll need different tokens (the {0}) in different languages.

提交回复
热议问题