Like this
string[] strarry = [12, 34, A1, FE, 12, 34, EA, 0, FE]
12 is the beginning , FE is the end, Turn into
string strarry = [[12,34,A1,FE],[12,34,EA,0,FE]];
The length from 12 to Fe is not necessarily
How can I do it? Thank you
CodePudding user response:
If I've understood you right (you want to obtain jagged array string[][]
from ordinal one - string[]
), you can try Linq in order to GroupBy
items into subarrays:
using System.Linq;
...
string[] strarry = new string[]
{"12", "34", "A1", "FE", "12", "34", "EA", "0", "FE"};
// Here we exploit side effects, be careful with them
int groupIndex = 0;
string[][] result = strarry
.GroupBy(value => value == "FE" ? groupIndex : groupIndex)
.Select(group => group.ToArray())
.ToArray();
Let's have a look:
string report = string.Join(Environment.NewLine, result
.Select(line => "[" string.Join(", ", line) "]"));
Outcome:
[12, 34, A1, FE]
[12, 34, EA, 0, FE]
CodePudding user response:
I am not sure how applyable this code is for you, but maybe it gets you somewhere. Best of luck!
var strarry = "12, 34, A1, FE, 12, 34, EA, 0, FE";
var splittedArrays = strarry.Split(", FE", StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < splittedArrays.Length; i )
{
if (splittedArrays[i][0].Equals(','))
{
splittedArrays[i] = splittedArrays[i].Substring(2);
}
splittedArrays[i] = ", FE";
Console.WriteLine(splittedArrays[i]);
}