12

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

5
  • 8
    In what way is that a string array? It looks more like an int[][] to me. Commented Aug 9, 2011 at 12:33
  • 2
    When you say 'Please suggest how can i do it.', are you sure you dont mean, 'Do it for me.' Commented Aug 9, 2011 at 12:34
  • Given that arrays are accessed by index, you can assume that the element at index i has an implicit i+1 as the first member of the list. Commented Aug 9, 2011 at 12:35
  • @Ash - some questions are just like that. It doesn't make them any less valid. "Please suggest how can I do it" is about as good as it gets Commented Aug 9, 2011 at 12:35
  • Yes it is a little surprising but you can do Array.insert msdn.microsoft.com/en-us/library/bb383995.aspx Commented Dec 2, 2017 at 9:35

7 Answers 7

19

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");
Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't it be a little cleaner to use: List<string> words = new List<string> {"Hello", "World"}; ?
16

Linq has an easy way to accomplish your "mission":

First add the correct namespace:

using System.Linq;

And then:

// Translate your splitted string (array) in a list:
var myList = myString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>();

// Where insertPosition is the position you want to insert in:
myList.Insert(insertingPosition, insertedString);

// Back to array:
string[] myArray = myList.ToArray<string>();

Hope this helps...

Comments

5

You cannot insert anything to Array. Use List<T> instead.

Comments

3

You have to use something like an ArrayList instead, which has an ArrayList.Insert() method.

   ArrayList myAL = new ArrayList();
   myAL.Insert( 0, "The" );
   myAL.Insert( 1, "fox" );
   myAL.Insert( 2, "jumps" );
   myAL.Insert( 3, "over" );
   myAL.Insert( 4, "the" );
   myAL.Insert( 5, "dog" );

Comments

3

well what if you have the list like the following

string a="66,45,34,23,39,83";
string b="64,46,332,73,39,33"; 
string c="54,76,32,23,96,42";

before splitting a,b,c...

string[] s=new string[]{a,b,c};

for(int i=0; i<s.length;i++){
    s[i]=(i+1)+s[i];
}

now split each string in s

you will have a list like

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

I am not sure if I have understood your problem or not. :|

Comments

1

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)

Comments

0

C# arrays cannot be resized. This means that you can't insert items.

You have two options:

  1. Use a List<T> instead.
  2. Create a new array, with one extra item in, copy the contents of the old one across and so on.

Option 1 is invariably to be preferred.


There is Array.Resize(T). On the face of it this contradicts what I state above. However, the documentation for this method states:

This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.