Sergio Tapia
Sergio Tapia

Reputation: 41128

Formatting my string

How can I format a string like this:

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

I remember I used to be able to put a comma at the end and specify the actual data to assign to {0},{1}, etc.

Any help?

Upvotes: 2

Views: 347

Answers (8)

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

Use string.Format as in

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

Upvotes: 1

Charles Bretana
Charles Bretana

Reputation: 146409

use string.format, and put individual format specifiers inside the braces, after the number and a 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 the single quotes to delineate literals, anything not inside braces will be output literally

Upvotes: 2

Guffa
Guffa

Reputation: 700152

An alternative is to use Join, if you have the values in a string array:

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

(Just wanted to be different from the 89724362 users who will show you how to use String.Format... ;)

Upvotes: 6

user10635
user10635

Reputation: 702

are you looking for:

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

would result in

"String with data Foo, Bar I wish to format"

Upvotes: 1

Philip Rieck
Philip Rieck

Reputation: 32568

Your question is a bit vague, but do 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?

Upvotes: 2

Mitch Wheat
Mitch Wheat

Reputation: 300489

The String.Format method accepts a format string followed by one to many variables that are to be formatted. The format string consists of placeholders, which are essentially locations to place the value of the variables you pass into the function.

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

Upvotes: 2

Matt Davis
Matt Davis

Reputation: 46034

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

Upvotes: 2

Alfred Myers
Alfred Myers

Reputation: 6453

Use string.Format method such as in:


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

Upvotes: 15

Related Questions