简体   繁体   中英

string concatenation

HI, I want to dynamically concatenating the strings using C#. I have localized string in XML file , this string i want to update based on the language selection at runtime.
Below i specified input string and expected output string formats.

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:"

Thanks

You're looking for 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);

Unlike C's printf function, which relies on the parameters being supplied in the order in which they'll be substituted, string.Format requires that you explicitly indicate which parameter goes where. In other words, {0} means that the first (0-index) parameter will be substituted there.

You can optionally specify a format string (useful for numbers and dates and such) like this: {1:0.00} . This means the second (index 1) element with a format string of "0.00" (whatever that might mean for the type in question).

string output = "The density of your "  + gas.ToString() + " gas at reference conditions of " + weightG.ToSTring() + "g and " + weightKg.ToSTring() + " Kg is:";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM