2

i searched for a method to split strings and i found one.
Now my problem is that i can´t use the method like it is described.

Stackoverflow answer

It is going to tell that i

cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'string[]'.

The provided method is:

public static class EnumerableEx
{
    public static IEnumerable<string> SplitBy(this string str, int chunkLength)
    {
        if (String.IsNullOrEmpty(str)) throw new ArgumentException();
        if (chunkLength < 1) throw new ArgumentException();

        for (int i = 0; i < str.Length; i += chunkLength)
        {
            if (chunkLength + i > str.Length)
                chunkLength = str.Length - i;

            yield return str.Substring(i, chunkLength);
        }
    }
}

How he said it is used:

string[] result = "bobjoecat".SplitBy(3); // [bob, joe, cat]
Community
  • 1
  • 1
Chakratos
  • 49
  • 1
  • 10
  • arrays do not implement IEnumerable – Amit Kumar Ghosh Jun 23 '15 at 10:09
  • 2
    @AmitKumarGhosh You can assign a `string[]` to an `IEnumerable` The problem is the opposite: you cannot assign an `IEnumerable` to a `string[]` – Dennis_E Jun 23 '15 at 10:16
  • 1
    @AmitKumarGhosh, [Arrays do implement IEnumerable](https://ideone.com/d8rBt6), since .NET Framework 2.0, but only at [run-time](https://msdn.microsoft.com/en-us/library/system.array.aspx#remarksToggle). However, as mentioned by Dennis_E, this is not the root of the problem. – awesoon Jun 23 '15 at 10:20
  • @soon: it's also [documented](https://msdn.microsoft.com/en-us/library/system.array.aspx): _"Starting with the .NET Framework 2.0, the Array class implements the System.Collections.Generic.IList, System.Collections.Generic.ICollection, and System.Collections.Generic.IEnumerable generic interfaces. The implementations are provided to arrays at run time..."_ – Tim Schmelter Jun 23 '15 at 10:36
  • Sure, and my comment has the same link under `run-time` words. – awesoon Jun 23 '15 at 10:40

3 Answers3

8

You have to use ToArray() method:

string[] result = "bobjoecat".SplitBy(3).ToArray(); // [bob, joe, cat]

You can implicitly convert Array to IEnumerable but cannot do it vice versa.

kamil-mrzyglod
  • 4,948
  • 1
  • 20
  • 29
1

Note that you could even modify directly the method to return a string[]:

public static class EnumerableEx
{
    public static string[] SplitByToArray(this string str, int chunkLength)
    {
        if (String.IsNullOrEmpty(str)) throw new ArgumentException();
        if (chunkLength < 1) throw new ArgumentException();

        var arr = new string[(str.Length + chunkLength - 1) / chunkLength];

        for (int i = 0, j = 0; i < str.Length; i += chunkLength, j++)
        {
            if (chunkLength + i > str.Length)
                chunkLength = str.Length - i;

            arr[j] = str.Substring(i, chunkLength);
        }

        return arr;
    }
}
xanatos
  • 109,618
  • 12
  • 197
  • 280
0

If somehow you end up with this: IEnumerable<string> things = new[] { "bob", "joe", "cat" }; you can transform it into string[] like this: string[] myStringArray = things.Select(it => it).ToArray();

Gabriel Marius Popescu
  • 2,016
  • 2
  • 20
  • 22