Insert a element in String array

Viewed 99858

I have string containing n elements.

I want to insert a automatically incremented number to the first position.

E.g Data

66,45,34,23,39,83
64,46,332,73,39,33 
54,76,32,23,96,42

I am spliting to string to array with split char ','

I want resultant array with a incremented number a first position

1,66,45,34,23,39,83
2,64,46,332,73,39,33
3,54,76,32,23,96,42

Please suggest how can I do it.

Thanks

7 Answers

I know you want a C# answer (and there are good answers here already in that language), but I'm learning F# and took this on in that language as a learning exercise.

So, assuming you want an array of strings back and are willing to use F#...

let aStructuredStringArray = [|"66,45,34,23,39,83"
                               "64,46,332,73,39,33"
                               "54,76,32,23,96,42"|]

let insertRowCountAsString (structuredStringArray: string array) =
    [| for i in [0 .. structuredStringArray.Length-1] 
        do yield String.concat "" [ i.ToString(); ","; structuredStringArray.[i]]
            |]

printfn "insertRowCountAsString=%A" (insertRowCountAsString aStructuredStringArray)
Related