Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a element in String array

Tags:

arrays

c#

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

like image 678
usr021986 Avatar asked Aug 09 '11 12:08

usr021986


People also ask

How do you add elements to a String array?

By using ArrayList as intermediate storage: Create an ArrayList with the original array, using asList() method. Simply add the required element in the list using add() method. Convert the list to an array using toArray() method.

Can we insert String in array?

Since Java arrays hold a fixed number of values, you need to create a new array with a length of 5 in this case. A better solution would be to use an ArrayList and simply add strings to the array.


1 Answers

You can't with an array, you need to use a List<string> instead.

For example:

List<string> words = new string[] { "Hello", "world" }.ToList();
words.Insert(0, "Well");
like image 50
Kieren Johnstone Avatar answered Oct 05 '22 12:10

Kieren Johnstone