2

Let's say I have an array for example

string[] A = {"1","2","3","4","5"}

I want the array to be of size 10, and want to insert blank strings after a certain index.

For example, I could make it size 10 and insert strings after index 3 which would result in

A = {"1","2","3","4","","","","","","5"}

Basically the elements after the given index will be pushed to the end and blank strings will take the empty space in between.

This is what I tried but it only adds one string and doesnt exactly set a size for the array

var foos = new List<string>(A);
foos.Insert(33, "");
foos[32] = "";
A = foos.ToArray();
Sayse
  • 42,633
  • 14
  • 77
  • 146
sparta93
  • 3,684
  • 5
  • 32
  • 63

5 Answers5

5

You can use InsertRange

var l = new List<string>{"1","2","3","4","5"};
l.InsertRange(3, new string[10 - l.Count]);
foreach(var i in l)
    Console.WriteLine(i);

Note: The above doesn't populate with empty strings but null values, but you can easily modify the new string[] being used to be populated with your desired default.

For example; see How to populate/instantiate a C# array with a single value?

Community
  • 1
  • 1
Sayse
  • 42,633
  • 14
  • 77
  • 146
3

Here is LINQ based approach:

public string[] InsertToArray(string[] array, int index, int newArrayCapacity)
{
    var firstPart = array.Take(index + 1);
    var secondPart = array.Skip(index + 1);
    var newPart = Enumerable.Repeat(String.Empty, newArrayCapacity - array.Length);
    return firstPart.Concat(newPart).Concat(secondPart).ToArray();
}

Here is the usage of the method:

string[] A = {"1","2","3","4","5"};

// Insert 5 elements (so that A.Length will be 10) in A after 3rd element
var result = InsertToArray(A, 3, 10);


Added: see Sayse's answer, really the way to go

Alex Sikilinda
  • 2,928
  • 18
  • 34
1

Since arrays are fixed sized collections, you can't resize an array.What you need to do is to split your array elements, you need to get the elements before and after the specified index,you can do that by using Skip and Take methods, then you need to generate a sequence of empty strings and put them together:

string[] A = {"1","2","3","4","5"};

int index = 3;

var result = A.Take(index + 1)
              .Concat(Enumerable.Repeat("", 10 - A.Length))
              .Concat(A.Skip(index+1))
              .ToArray();
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

If you don't mind using a list instead of an array, you can do it this way:

int index = 3;
int numberOfBlanksToInsert = 5;
List<string> strings = new List<string>();
for (int i = 0; i < numberOfBlanksToInsert; i++)
{
    strings.Insert(index, "");
}

You can also output this to an array when you're done:

string[] A = strings.ToArray();
oppassum
  • 1,746
  • 13
  • 22
0
    static string[] InsertRange(string[] initialValues, int startIndex, int count, string toInsert)
    {
        string[] result = new string[initialValues.Length + count];
        for (int i = 0; i < initialValues.Length + count; i++)
            result[i] = i < startIndex ? initialValues[i] : i >= startIndex + count ? initialValues[i - count] : toInsert;
        return result;
    }

Usage : InsertRange(A, 4, 5, "hello");

Output : "1, 2, 3, 4, hello, hello, hello, hello, hello, 5"

Fabjan
  • 13,506
  • 4
  • 25
  • 52