18

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.

1
  • 1
    why is your former solution not good - it looks perfect to me! :) Commented Apr 27, 2012 at 8:42

6 Answers 6

29
string.Concat(Enumerable.Range(1,10).Select(i => string.Format("[{0}]", i)))
Sign up to request clarification or add additional context in comments.

1 Comment

Just a side note: this works only in framework >= 4. For the older versions you need to turn Concat argument into an array, for example adding .ToArray()
17

I wanted something like this, but with a possibility to enter a format string and a separator. So this is what I came up with:

public static string JoinFormat<T>(this IEnumerable<T> list, string separator,
                                   string formatString)
{
    formatString = string.IsNullOrWhiteSpace(formatString) ? "{0}": formatString;
    return string.Join(separator,
                       list.Select(item => string.Format(formatString, item)));
}

Now you could make a list like

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

by entering numbers.JoinFormat(", ", "[{0}]").

Whereas a Concat solution with "[{0}],") would have a trailing comma.

An empty or null separator produces your list.

1 Comment

I believe this should be an answer. It's universal.
1

You are probably looking for a LINQ solution such as

string nums = String.Concat(Enumerable.Range(1, 10)
                                      .Select(i => string.Format("[{0}]", i)))

Comments

1
StringBuilder str = new StringBuilder();
for (int i = 1; i <= 10; i++)
    str.AppendFormat("[{0}]", i);

Console.WriteLine(str.ToString());

My recommendation is to use StringBuilder to append the same pattern.

Comments

1

I'd just concatenate each item, and use String.Concat to put them together:

string num =
  String.Concat(
    Enumerable.Range(1, 10).Select(n => "[" + n + "]")
  );

If you want to get fancy, you can make a cross join between the numbers and a string array. :)

string num =
  String.Concat(
    from n in Enumerable.Range(1, 10)
    from s in new string[] { "[", null, "]" }
    select s ?? n.ToString()
  );

Comments

0
string.Join(',', Enumerable.Range(1, 10).Select(i => string.Format("'{0}'", i)))

String Join can be better option than Concat

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.