N3wbie
N3wbie

Reputation: 227

split a string array to a jagged object array

I want to make a string array with values of names and some numbers(which are strings) i want to pass them into a function that will take the array and then split them into an object jagged array (1 array of strings and 1 array of ints)
the array is:

string[] str= { "toto", "the", "moto", "my", "friend","12","13","14","99","88"};

and the function looks like this:

public object[][] bloop (string[] bstr)
{

}

whats next?

Upvotes: 0

Views: 269

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186803

Linq solution.

You have two groups: first one has items that can be parsed to int and the second group contains all the others, so GroupBy looks quite naturally:

public Object[][] bloop(string[] bstr) {
  if (null == bstr)
    throw new ArgumentNullException("bstr");

  int v;

  return bstr
    .GroupBy(x => int.TryParse(x, out v))
    .OrderBy(chunk => chunk.Key) // let strings be the first
    .Select(chunk => chunk.ToArray())
    .ToArray();
}

Test:

string[] str = { "toto", "the", "moto", "my", "friend", "12", "13", "14", "99", "88" };

// toto, the, moto, my, friend
// 12, 13, 14, 99, 88
Console.Write(String.Join(Environment.NewLine, 
   bloop(str).Select(x => String.Join(", ", x))));

Upvotes: 0

Siva Kumar Siddam
Siva Kumar Siddam

Reputation: 154

public static object[][] bloop(string[] bstr)
    {
        object[][] result = new object[2][] { new object[bstr.Length], new object[bstr.Length] };
        int sFlag = 0, iFlag = 0, val;            
        foreach (string str in bstr)
            if (int.TryParse(str, out val))
                result[1][iFlag++] = val;
            else
                result[0][sFlag++] = str;
        return result;
    }

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460228

I agree that your requirement sounds odd and should be solved with a different approach. However, this will do what you want:

public T[][] Bloop<T>(T[] items)
{
    if (items == null) throw new ArgumentNullException("items");
    if (items.Length == 1) return new T[][] { items, new T[] { } };

    int firstLength = (int) Math.Ceiling((double)items.Length / 2);
    T[] firstPart = new T[firstLength];
    Array.Copy(items, 0, firstPart, 0, firstLength);
    int secondLength = (int)Math.Floor((double)items.Length / 2);
    T[] secondPart = new T[secondLength];
    Array.Copy(items, firstLength, secondPart, 0, secondLength);
    return new T[][] { firstPart, secondPart };
}

Your sample:

string[] str= { "toto", "the", "moto", "my", "friend","12","13","14","99","88"};
string[][] result = Bloop(str);

If you need the second array as int[] you could use following:

int[] ints = Array.ConvertAll(result[1], int.Parse);

Upvotes: 0

Vadim Martynov
Vadim Martynov

Reputation: 8892

Your scenario looks like bad design that can cause errors and performance issues. The better way is to change code for using generic List<> or something like that. But in your current problem you can use below code:

public object[][] bloop (string[] bstr)
{
    var numbers = new List<int>();
    var strings = new List<string>();
    var result = new object[2][];

    foreach(var str in bstr)
    {
        int number = 0;
        if(int.TryParse(str, out number))
        {
            numbers.Add(number);
        }
        else
        {
            strings.Add(str);
        }
    }

    result[0] = strings.ToArray();
    result[1] = numbers.ToArray();

    return result;
}

Upvotes: 3

Related Questions