1

How can I extract this whole string into 5 columns?

"1;2;3;4;5;A;AAA;AA;AAAA;AAAAA;NA;NA;PASS;YES;NO;test1;test2;test3;test4;test5;Word4;Word5;Word3;Word2;Word1"

Like this:

enter image description here

And I want to put the final result in a List by rows;

eg:

result 1 = enter image description here

result 2 = enter image description here

redbeef
  • 43
  • 7
  • What have you tried and what isn't working? If you know the separator then you can use `string.Split()` to break it apart into individual elements. If the column count is static then you can divide the number of elements by that count to get the count of rows. With that value you can loop over the elements and add them to your matrix. Where are you stuck? – David Nov 03 '20 at 13:54
  • What is the final result type supposed to be? Are you looking to have this in a data table? List of string? One string per row? Need some clarity to be able to help, otherwise you're going to get answers for every possible version and none of them will be right for you. – gilliduck Nov 03 '20 at 13:57
  • Why are you working with delimited strings in the first place? – Zohar Peled Nov 03 '20 at 14:05

2 Answers2

3

The question does not stipulate a final data structure. Let's suppose we want something like this at the end:

List<string[]>
{
    string[]{ "1", "A", "NA", "test1", "Word4" }, 
    string[]{ "2", "AAA", "NA", "test2", "Word5" },
    ...
}

A better structured data structure would be more suitable.

And let's suppose that the input string does not contain words having the separator ;

var data = "1;2;3;4;5;A;AAA;AA;AAAA;AAAAA;NA;NA;PASS;YES;NO;test1;test2;test3;test4;test5;Word4;Word5;Word3;Word2;Word1";
var splitData = data.Split(';');
var columnCount = 5;

// We check that the input data has the right number of elements (multiple of columnCount).
if (splitData.Length % columnCount != 0)
{
    throw new InvalidOperationException();
}

var results = splitData
    .Select((columnVal, i) => (columnVal, i)) // projection of each element including its index.
    .GroupBy( x => x.i % columnCount, // group elements by 'row' [0,5,10,15,20][1,6,11,16,21][2,7,12,17,22]...
              x => x.columnVal, // selecting only the value (we don't need the index anymore).
              (_, columns) => columns.ToArray()) // putting all column values for each row in an array, here we could map to a different data structure.
    .ToList();

// the 'results' variable has the target data structure.

If we display the parsed data on the console:

for (var i = 0; i < results.Count; i++)
{
    var row = results[i];

    // each column value 'i' is in 'result[i]'
    var columnStr = string.Join(" | ", row);
    Console.WriteLine($"Row {i} -> {columnStr}");
}

/*
Row 0 -> 1 | A | NA | test1 | Word4
Row 1 -> 2 | AAA | NA | test2 | Word5
Row 2 -> 3 | AA | PASS | test3 | Word3
Row 3 -> 4 | AAAA | YES | test4 | Word2
Row 4 -> 5 | AAAAA | NO | test5 | Word1
*/
polkduran
  • 2,533
  • 24
  • 34
1

Here is the solution with assertion tests

    string input = "1;2;3;4;5;A;AAA;AA;AAAA;AAAAA;NA;NA;PASS;YES;NO;test1;test2;test3;test4;test5;Word4;Word5;Word3;Word2;Word1";
    var elementSplit = input.Split(';');
    int columnNumber = 5;
    List<string> result = new List<string>();
    for (int columnIndex = 0; columnIndex < columnNumber; columnIndex++)
    {
        StringBuilder row = new StringBuilder();
        for (int i = columnIndex; i < elementSplit.Length; i+= columnNumber)
        {
            row.Append(elementSplit[i]);
            row.Append(";");
        }
        row.Remove(row.Length - 1, 1);
        result.Add(row.ToString());
    }
    Assert.AreEqual("1;A;NA;test1;Word4", result[0]);
    Assert.AreEqual("2;AAA;NA;test2;Word5", result[1]);
hsd
  • 452
  • 5
  • 12
  • Any suggestion why -1? The code is doing what is expected. – hsd Nov 03 '20 at 13:56
  • We don't know what is expected. You output it as strings in a list, could be a data table, could be list> for all we know. Without more info, this answer is just a stab in the dark guess. – gilliduck Nov 03 '20 at 13:57
  • @KrishnaMuppalla first of all this is my code. Second I modify it to work with different data. – hsd Nov 03 '20 at 14:03
  • @gilliduck this sound like question to author not to me :) – hsd Nov 03 '20 at 14:04