字符串连接

Rav*_*avi 0 c# xml string

嗨,我想用C#动态连接字符串.我在XML文件中有本地化的字符串,这个字符串我想根据运行时的语言选择进行更新.
下面我指定了输入字符串和预期输出字符串格式.

EX:
  *Input String:*
      "The density of your %s gas at reference conditions of %s  %s and %s  %s is:"
  *Expected Output String:*
    "The density of your Helium gas at reference conditions of 20.01  g and 15.12  Kg is:"
Run Code Online (Sandbox Code Playgroud)

谢谢

Ada*_*son 6

你在找string.Format.

string output = string.Format(
"The density of your {0} gas at reference conditions of {1} {2} and {3} {4} is:", 
    gas, condition1, condition2, condition3, condition 4);
Run Code Online (Sandbox Code Playgroud)

与C printf函数不同,C 函数依赖于按照它们被替换的顺序提供的参数,string.Format要求您明确指出哪个参数在哪里.换句话说,{0}意味着第一个(0索引)参数将在那里被替换.

您可以选择指定格式字符串(对数字和日期等有用),如下所示:{1:0.00}.这意味着第二个(索引1)元素的格式字符串为"0.00"(对于所讨论的类型可能意味着什么).