Answered by:
Converting group comma delimited string into 2D Array

Question
-
User23738171 posted
Can anyone help me to convert group Comma Delimited String into 2D Array. please give me small example for better understanding. Thank in advance.Wednesday, June 26, 2019 11:03 AM
Answers
-
User-1038772411 posted
Hello Saraqueen,
Kindly refer the below link, I hope this will help you to understand
Thank you.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 26, 2019 11:11 AM -
User-719153870 posted
Hi Saraqueen,
To convert a string into a two-dimensional array, you can first use the Split() method to split the string into Int-type values and store them in a one-dimensional array.
Then read the values in a one-dimensional array and store them in a two-dimensional array.
You can refer to below codes in a C# console demo:
static void Main(string[] args) { int r = 2, c = 5, n = 20; string str = "1,2,3,4,5,6,7,8,9,0"; string[] Arr = str.Split(',');//Separating strings based on commas int[] IArr = new int[n];//Define one-dimensional array int[,] CommArr = new int[r, c];//Define two-dimensional array for (int i = 0; i < Arr.Length; i++) { IArr[i] = Convert.ToInt32(Arr[i]);//Store values in one-dimensional array } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { CommArr[i, j] = IArr[i * c + j];//Store values in two-dimensional array Console.Write(CommArr[i, j] + " ");//display data } Console.WriteLine("\n"); } }
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 27, 2019 5:13 AM
All replies
-
User-1038772411 posted
Hello Saraqueen,
Kindly refer the below link, I hope this will help you to understand
Thank you.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 26, 2019 11:11 AM -
User-719153870 posted
Hi Saraqueen,
To convert a string into a two-dimensional array, you can first use the Split() method to split the string into Int-type values and store them in a one-dimensional array.
Then read the values in a one-dimensional array and store them in a two-dimensional array.
You can refer to below codes in a C# console demo:
static void Main(string[] args) { int r = 2, c = 5, n = 20; string str = "1,2,3,4,5,6,7,8,9,0"; string[] Arr = str.Split(',');//Separating strings based on commas int[] IArr = new int[n];//Define one-dimensional array int[,] CommArr = new int[r, c];//Define two-dimensional array for (int i = 0; i < Arr.Length; i++) { IArr[i] = Convert.ToInt32(Arr[i]);//Store values in one-dimensional array } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { CommArr[i, j] = IArr[i * c + j];//Store values in two-dimensional array Console.Write(CommArr[i, j] + " ");//display data } Console.WriteLine("\n"); } }
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 27, 2019 5:13 AM