Formatting my string

How can I format the string like this:

string X = "'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}'",????

      

I remember that earlier I could put a comma at the end and give the actual data to assign to {0}, {1}, etc.

Any help?

+2


source to share


8 answers


Use string.Format method like:




string X = string.Format("'{0}','{1}','{2}'", foo, bar, baz);

      

+15


source


An alternative is to use Join if you have values ​​in an array of strings:

string x = "'" + String.Join("','", valueArray) + "'";

      



(Just wanted to be different from users 89724362 showing you how to use String.Format ...;)

+6


source


String.Format("'{0}', '{1}'", arg0, arg1);

      

+2


source


The method String.Format

accepts a format string followed by one to many variables to be formatted. The format string is made up of placeholders, which are essentially places to place the values ​​of the variables that you pass to the function.

Console.WriteLine(String.Format("{0}, {1}, {2}", var1, var2, var3));

      

+2


source


Your question is a little vague, but you mean:

// declare and set variables val1 and val2 up here somewhere
string X = string.Format("'{0}','{1}'", val1, val2);

      

Or are you asking for something else?

+2


source


use string.format and put the individual format specifiers inside curly braces, after the number and colon, as in

   string s = string.Format(
        " {0:d MMM yyyy} --- {1:000} --- {2:#,##0.0} -- {3:f}",
        DateTime.Now, 1, 12345.678, 3e-6);

      

and as you can see from the example, you don't need single quotes to delimit literals, anything not inside curly braces will be output literally

+2


source


Use string.Format

as in

var output = string.Format("'{0}', '{1}'", x, y);

      

+1


source


You are looking for:

String.Format("String with data {0}, {1} I wish to format", "Foo", "Bar");

      

will lead to

"Data string Foo, Bar I want to format"

+1


source







All Articles