0

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 distance from 12 to FE is not necessarily fixed (4)

How can I do it? Thank you

ShuSong Lu
  • 33
  • 5
  • 2
    And what have you tried, what is the problem? – Johan Donne Oct 12 '21 at 06:13
  • 3
    "The length from 12 to Fe is not necessarily"... is not necessarily what? What is the criterium for dividing the area? Is the length of `strarry` always the same (9 elements)? And you always want to have 4 elements in the first subarray and 5 in the second? – TN888 Oct 12 '21 at 06:13
  • 2
    When you say "two-bit" array, you presumably mean two-dimensional array, right? – ProgrammingLlama Oct 12 '21 at 06:15
  • Is two-dimensional array;God,Is my fault – ShuSong Lu Oct 12 '21 at 06:23
  • beginning is 12; ending is FE .There are not necessarily Four units; sorry :( – ShuSong Lu Oct 12 '21 at 06:27
  • 1
    You can take a look at how to partition a list, here: https://stackoverflow.com/a/1396143/1817574. Converting the result to a 2D array should not be hard. –  Oct 12 '21 at 06:27

2 Answers2

2

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) + "]"));

  Console.Write(report);

Outcome:

[12, 34, A1, FE]
[12, 34, EA, 0, FE]
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

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]);
        }
Patrick
  • 387
  • 3
  • 15