-1

From this a whole string;

"1;2;3;4;5;2;2;2;2;2;3;3;3;3;3;4;4;4;4;4;5;5;5;5;5"

How could I achieve to put the in array like this;

enter image description here

I want to put them in an array by row.

Stefan
  • 17,448
  • 11
  • 60
  • 79
Jesson
  • 289
  • 1
  • 5
  • 13
  • 1
    Is your desired output a two dimensional array? With the first entry being an array containing the values `1,2,3,4,5`, the second `2,2,2,2,2`? – RedFox Nov 03 '20 at 12:01
  • https://stackoverflow.com/questions/12389203/how-do-i-chunk-an-enumerable, https://stackoverflow.com/questions/419019/split-list-into-sublists-with-linq, https://stackoverflow.com/questions/13731796/create-batches-in-linq – xdtTransform Nov 03 '20 at 12:25
  • And https://stackoverflow.com/questions/7073343/list-to-two-dimensional-array – xdtTransform Nov 03 '20 at 12:27

4 Answers4

1

You can group your string into list of arrays each containing 5 elements from your source string with the following code:

 private void Test()
 {
     var str = "1;2;3;4;5;2;2;2;2;2;3;3;3;3;3;4;4;4;4;4;5;5;5;5;5";
     var items = str.Split(';').Select(x => int.Parse(x)).ToList();
     var result = ChunkBy(items, 5);
 }

 private List<T[]> ChunkBy<T>(List<T> list, int count)
 {
      var result = new List<T[]>();

      var groupsCount = list.Count / count;

      while (result.Count < groupsCount)
      {
           result.Add(list.Skip(count * result.Count).Take(count).ToArray());
      }

      return result;
}
Sergey Anisimov
  • 885
  • 8
  • 22
1

You can have result as string with expected result as you mentioned by row

string input = "1;2;3;4;5;2;2;2;2;2;3;3;3;3;3;4;4;4;4;4;5;5;5;5;5";
input = input + ";";
int columnNumber = 5;
List<string> result = new List<string>();
for (int columnIndex = 0; columnIndex < columnNumber; columnIndex++)
{
    StringBuilder row = new StringBuilder();
    for (int i = columnIndex * 2; i < input.Length; i+= columnNumber * 2)
    {
        row.Append(input.Substring(i, 2));
    }
    row.Remove(row.Length - 1, 1);
    result.Add(row.ToString());
}
Assert.AreEqual("1;2;3;4;5", result[0]);
Assert.AreEqual("2;2;3;4;5", result[1]);

You can letter on work on form of results string collection like ; or ,. Additionally I add some assertions to check results(you can remove it)

hsd
  • 452
  • 5
  • 12
0

With a 2 dimensional array:

var subject = "1;2;3;4;5;2;2;2;2;2;3;3;3;3;3;4;4;4;4;4;5;5;5;5;5";
var splittedSubject = subject.Split(new[]{';'});
        
int columns = 3;
int rows = splittedSubject.Length / columns + 1;
        
var twoDimArray = new int[columns,rows];
        
var index = 0;
foreach(var c in splittedSubject)
{
    twoDimArray[index % columns, index / columns] = int.Parse(splittedSubject[index]);
    index++;
}

Or as such, as lists:

int columns = 3;
List<int>[] list = new List<int>[] { new List<int>(), new List<int>(),new List<int>()};
                
var subject = "1;2;3;4;5;2;2;2;2;2;3;3;3;3;3;4;4;4;4;4;5;5;5;5;5";
        
var splitted = subject.Split(new[]{';'});

var index = 0;
foreach(var c in splitted)
{
    list[index % columns].Add(int.Parse(splitted[index]));
    index++;
}
Stefan
  • 17,448
  • 11
  • 60
  • 79
0

I assume that you mean to put "the ever first five character" into the fist column, the next five into the second column and so on.

First convert the input string into an array containing the numbers (as strings).

Then convert it to a 2-d array of ints:

public static int[,] ToMatrix(string input)
{
    const int Rows = 5;

    string[] numbers = input.Split(';');
    int columns = (numbers.Length + Rows - 1) / Rows;
    int[,] matrix = new int[Rows, columns];

    int i = 0;
    for (int col = 0; col < columns; col++) {
        for (int row = 0; row < Rows; row++, i++) {
            // In case numbers.Length is not divisible by 5.
            if (i >= numbers.Length) return matrix;

            matrix[row, col] = Int32.Parse(numbers[i]);
        }
    }
    return matrix;
}

You could also choose to not handle the case where the input length is not divisible by 5 and to drop extra numbers. Then the column number would simply be:

int columns = numbers.Length / Rows;

and the if (i >= numbers.Length) return matrix; could be dropped.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188