Shachaf.Gortler
Shachaf.Gortler

Reputation: 5745

Reformat and build a string of consecutive numbers

I have string I want to format with consecutive numbers , what i have done is

int i =0 

String res = String.Foramt("text {0}  text {1} {2} {3} {4} ...", i
         ,i+1 , i+2, i+3, i+4)

is there a more elegant way of doing this , using Regex or Linq string manipulations ?

Upvotes: 2

Views: 79

Answers (2)

k-sunako
k-sunako

Reputation: 26

        string res2 = string.Format(
            "text {0}  text {1} {2} {3} {4} ...", 
            Enumerable.Range(0, 5).OfType<object>().ToArray());

string.Format takes object array argument.

see https://msdn.microsoft.com/en-us/library/w5zay9db.aspx;

Upvotes: 1

Evk
Evk

Reputation: 101553

There are many different ways to do that, but I think

String.Join(" ", Enumerable.Range(0, 4))

is elegant enough for what you are trying to achieve.

Upvotes: 4

Related Questions