views:

84

answers:

3

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

+6  A: 

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).

Adam Robinson
It is very useful for me. Thanks
Ravi
@Ravi: If this solves your problem, please be sure to accept this answer. You might also consider going back to some of your older questions and marking the answer to those as well.
Adam Robinson
+1  A: 

http://msdn.microsoft.com/library/system.string.format(VS.90).aspx

Andrey
It is very useful for me. Thanks
Ravi
A: 
string output = "The density of your "  + gas.ToString() + " gas at reference conditions of " + weightG.ToSTring() + "g and " + weightKg.ToSTring() + " Kg is:";
Dima
How exactly would this work with localised strings stored in an xml file?
ck