3

I am trying to declare an array of size 3, then use the string split() method to assign values to each index of the array. However, the Split() method seems to override the size of the array and sets its size to the amount of substrings the Split() method comes out with.

So This is what I did,

string myString = "Hello world";
string[] myArray = new string[3];

for(int i = 0; i < 3; i++)
    myArray[i] = "";

myArray = myString.Split(' ');

But once again, when I use the Split() method, it overrides the size of my array and sets it to 2 which causes complications for me later on.

So I need to know how to use the split method to add strings into the array of size 3. If the array contains less than 3 items, I want to set the unassigned indexes to "" and if there are more than 3 substrings then I want to only take the first three and discard the rest.

Thanks in advance for any help guys.

2
  • Why not fix it on the other end? Just check the length of it. Commented Feb 11, 2016 at 21:56
  • It may be worth creating an extension method for this. Check my answer below. Commented Feb 11, 2016 at 22:27

3 Answers 3

4

You can set the result of the string split to a new array, transferring the contents to the original array in a loop. If the string split array is longer than the original array then the original should be set to the new one in order to avoid an out of bounds exception.

var splitArray = myString.Split(' ');
if(splitArray.Length >= myArray.Length)
{
    myArray = splitArray;
}
else
{
    for(int i = 0; i < splitArray.Length; i++)
        myArray[i] = splitArray[i];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Worked great. The only small change I had to make was add a for loop to the if body that loops from 0 -> myArray.Length so the myArray won't take more than 3 items when the splitArray is greater than 3. Thanks a lot.
2

Just needs a little re-jig (updated to cope with the case where the myArray is smaller than mySplitString):

string myString = "Hello world";
string[] mySplitString = myString.Split(' ');

string[] myArray = new string[3];
var loopLength = Math.min(mySplitString.length,myArray.length);

for(int i = 0; i < loopLength; i++){
    myArray[i] = mySplitString[i];
}

1 Comment

Array.Copy would be an option too.
0

Here is another idea; I have used an extension method called ConsistantSplit here. So you can split the string in to a consistent size array.

    private static void Main()
    {
        string myString = "Hello world";
        var mySplitedString = myString.ConsistantSplit(' ', 5);
        Console.WriteLine(mySplitedString.Length);
        Console.ReadLine();
    }

    private static string[] ConsistantSplit(this string text, char splitter, int size)
    {
        string[] consistantSplitter = new string[size];
        var splitted = text.Split(splitter);
        if (size < splitted.Length)
            return splitted.Take(size).ToArray();
        splitted.CopyTo(consistantSplitter, 0);
            return consistantSplitter;
    }

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.