0

I have a message coming containing 48 different values seperated as:

String values = "4774,55567,44477|14555,4447,5687|416644,4447,5623|...";

I need to convert it into 16 by 3 array as:

String[,] vals = {{"4774,55567,44477"},{14555,4447,5687},{416644,4447,5623}...}

I tried using the split() function but cannot figure how to feed it into the matrix

2
  • You are asking to get a multidimensional array here ([,]) but for all practically purposes you can just use a jagged array ([],[]) With the limited info provided by your question I could suggest to use a jagged array because you get the advantage to convert your string to a jagged array with just one line of code Commented Jan 15, 2023 at 10:49
  • Try : String[][] vals = values.Split(new char[] { '|' }) .Select(x => x.Split(new char[] { ',' }).ToArray()).ToArray(); Commented Jan 15, 2023 at 11:35

5 Answers 5

1

You can split it two times:

var values = "4774,55567,44477|14555,4447,5687|416644,4447,5623";
var rows = values.Split('|');
var matrix = new string[rows.Length][];
for (var i = 0; i < rows.Length; i++)
{
    matrix[i] = rows[i].Split(',');
}

and a more elegant solution using LINQ:

var values = "4774,55567,44477|14555,4447,5687|416644,4447,5623";
var data = values
    .Split('|')
    .Select(r => r.Split(','))
    .ToArray();

EDIT: as @RandRandom pointed out, the solution above creates a jagged array. The difference is explained here: link. If the jagged array is not an option for you, to create a 2d array you need to create a matrix, specifying the dimensions [rows.Length, rows[0].Split(',').Length] and with the help of 2 for-loops assign the values:

string values = "4774,55567,44477|14555,4447,5687|416644,4447,5623";
string[] rows = values.Split('|');
string[,] matrix = new string[rows.Length, rows[0].Split(',').Length];
for (int i = 0; i < rows.Length; i++)
{
    string[] rowColumns = rows[i].Split(',');
    for (int j = 0; j < rowColumns.Length; j++)
    {
        matrix[i, j] = rowColumns[j];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Though it is a jagged array and not a multidimensional array (2d)
0

You can split the string based on the | character. Then, using linq.Select(), split each line based on , and create a two-dimensional array

string[] temp = values.Split('|');
var res=temp.Select(x => x.Split(',')).ToArray();

Comments

0

Use Linq if you can consider the jagged array instead of 2D array:

var values = "4774,55567,44477|14555,4447,5687|416644,4447,5623";
var result = values
   .Split('|')
   .Select(x => x.Split(','))
   .ToArray();

Comments

0

You can use the String.Split() method to split the string into an array of substrings, and then use a nested loop to iterate through the array and add the values to the 2D array.

Here is an example :

string values = "4774,55567,44477|14555,4447,5687|416644,4447,5623|...";
string[] substrings = values.Split('|');
string[,] vals = new string[substrings.Length,3];
for (int i = 0; i < substrings.Length; i++)
{
    string[] subvalues = substrings[i].Split(',');
    for (int j = 0; j < subvalues.Length; j++)
    {
        vals[i, j] = subvalues[j];
    }
}

This will split the string values by the separator '|' and create an array of substrings. Then, it will iterate through each substring and split it again by ',' and store the result in a 2D array vals with 16 rows and 3 columns.

Comments

0

Try following :

            string values = "4774,55567,44477|14555,4447,5687|416644,4447,5623";
            String[][] vals = values.Split(new char[] { '|' }).Select(x => x.Split(new char[] { ',' }).ToArray()).ToArray();

            string[,] vals2 = new string[vals.GetLength(0), vals[0].Length];
            for(int i = 0; i < vals.GetLength(0); i++)
            {
                for(int j = 0; j < vals.Length; j++)
                {
                    vals2[i, j] = vals[i][j];
                }
            }

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.