2

I encountered a little problem that i can't get my head around. I have normal string[] array with a text import in it. Every row is stored in separate under every index. A row usually look something like this:

string[i] = |title1|title2|title3|title4|title5|
string[i+1] = |Word1|Word2|Word3|Word4|Word5|

I want to split these rows and put them in a multi dimensional array. Already counted how I have to declare the dimensions of the array. I want to split it now. I was thinking about going through the normal array with two loops and look for the separator while saving the words in a string then copying it to the multi array.

Do you guys have any idea how could i do that, because this is too much hassle for such a small thing.

I want the multi array look something like this:

string[0,0] = title1, 
string[0,1] = title2 etc. 
string[1,0] = word1 
string[1,1] = word2

This is the code that creates the array:

public string [,] SplitArrays(string [] arrayin)
{
    long columns = 0;
    char line = '|';

    string row;
    for(int i = 0; i < arrayin.Length;i++)
    {
        row = arrayin[i];
        if (Convert.ToChar(row.Substring(0, 1)) == line)
        {
            for(int j = 0; j < row.Length;j++)
            {
                if (Convert.ToChar(row.Substring(j,(j+1))) == line)
                {
                    columns++;
                }                           
            }
        }
        break;
    }
    int rowlength = arrayin.Length;
    string[,] finalarray = new string[columns,rowlength];

And this is how far I got with separating, but I got kind of confuse and I probably messed it up:

    int rowcolumncount = 0;
    string word = "";
    bool next = false;
    for(int k = 0; k < arrayin.Length; k++)
    {
        row = arrayin[k];

        for(int l = 0; l < row.Length; l++)
        {
            if (Convert.ToChar(row[l]) == line)
            {
                for(int z = 0; next == false;)
                {
                    if(row[z] == line)
                    {
                        next = true;
                    }
                    else
                    {
                        string part = Convert.ToString(row[z]);
                        word = string.Join("",part);
                    }
                    finalarray[l, rowcolumncount] = word;
                    rowcolumncount++;
                }
            }
            rowcolumncount = 0;
        }

    }

    return finalarray;
}

The main array contains around 12000 lines.

Thank you!

1
  • 1
    Hi, what exactly is wrong with your current code? Have you considered the Split function? Commented Oct 1, 2019 at 11:39

2 Answers 2

1

You can try something like this: Split each item with arrayin by | and write these chunks into a line of 2D array:

public string[,] SplitArrays(string[] arrayin) {
  if (null == arrayin)
    return null;
  else if (arrayin.Length <= 0)
    return new string[0, 0];

  // null : we don't know size (number of columns) before 1st line split
  string[,] result = null;

  int row = 0;

  foreach (var line in arrayin) {
    string[] items = line.Split('|');

    // - 2 : skip the very first and the very last items which are empty
    if (null == result)
      result = new string[arrayin.Length, items.Length - 2];

    // if line is too short, let's pad result with empty strings
    for (int col = 0; col < result.GetLength(1); ++col)
      result[row, col] = col + 1 < items.Length - 1 
        ? items[col + 1] 
        : "";            // padding

    row += 1;
  }

  return result;
}

Usage:

  string[] source = new string[] {
    "|title1|title2|title3|title4|title5|",
    "|Word1|Word2|Word3|Word4|Word5|",
  };

  // {
  //   {"title1", "title2", "title3", "title4", "title5"},
  //   { "Word1",  "Word2",  "Word3",  "Word4",  "Word5"}
  // }
  string[,] array = SplitArrays(source);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Works like butter. I had to remove some lines form the source files because the first couple lines included like creation date etc. which the method couldn't process.
0

If the number of items per line vary, you can use a jagged array.

We create the empty array and set the row count size.

Then we parse all lines of the list and for all line we split it to have desired items to add them into the dimension as we resize the row sub-array.

static public void Test()
{
  var list = new string[]
  {
    "| title1 | title2 | title3 | title4 | title5 |",
    "| Word1 | Word2 | Word3 | Word4 | Word5 |"
  };

  int indexD1 = 0;
  string[][] result = null;
  Array.Resize(ref result, list.Length);
  foreach ( string item in list )
  {
    var str = item;
    str = str.TrimStart('|').TrimStart();
    str = str.TrimEnd('|').TrimEnd();
    str = str.Replace(" | ", "|");
    var items = str.Split('|');
    Array.Resize(ref result[indexD1], items.Length);
    int indexD2 = 0;
    foreach ( string part in items )
      result[indexD1][indexD2++] = part;
    indexD1++;
  }

  foreach ( var row in result )
  {
    foreach ( var str in row )
      Console.WriteLine(str);
    Console.WriteLine();
  }
}

You can also use a List of List of Strings and use the method Add():

var result = new List<List<string>>();

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.