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();
0

5 Answers 5

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?

Sign up to request clarification or add additional context in comments.

1 Comment

This is not a good answer hes asking how to insert to an array not a list.
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

Comments

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();

Comments

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();

Comments

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"

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.