Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.Format with string.Join

I've tried making a string like this:

[1][2][3][4][5][6][7][8][9][10]

With this code:

string nums = "[" + string.Join("][", Enumerable.Range(1, 10)) + "]";

That, however, doesn't really look very good, so I was wondering if I could combine string.Format with string.Join, sort of like this:

string num = string.Join("[{0}]", Enumerable.Range(1, 10));

So that it wraps something around each item. However, that ends up like this:

1[{0}]2[{0}]3[{0}]4[{0}]5[{0}]6[{0}]7[{0}]8[{0}]9[{0}]10

Is there a better/easier way to do this?


Among all the solutions, I must say I prefer this

string s = string.Concat(Enumerable.Range(1, 4).Select(i => string.Format("SomeTitle: >>> {0} <<<\n", i)));

Over this

string s2 = "SomeTitle: >>>" + string.Join("<<<\nSomeTitle: >>>", Enumerable.Range(1, 4)) + "<<<\n";

Because all the formatting is done in one string, not in multiple.

like image 323
Brian Avatar asked Apr 27 '12 08:04

Brian


1 Answers

string.Concat(Enumerable.Range(1,10).Select(i => string.Format("[{0}]", i)))
like image 59
spender Avatar answered Sep 19 '22 03:09

spender