Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 615
Sergio Tapia Avatar asked Mar 11 '26 04:03

Sergio Tapia


2 Answers

Use string.Format method such as in:


string X = string.Format("'{0}','{1}','{2}'", foo, bar, baz);
like image 172
Alfred Myers Avatar answered Mar 12 '26 16:03

Alfred Myers


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

like image 22
Guffa Avatar answered Mar 12 '26 16:03

Guffa