0

How would I get all the lines between a line starting with a ">>" and a line that is equal to "!terminate". For example:

>> STARTING.POINT
text1
text2
text3
!terminate
>> STARTING.POINT.TWO
textA
textB
textC
!terminate

How do I convert this text into arrays A and B? Array A should be like this

>> STARTING.POINT
text1
text2
text3
!terminate

And array B should be

>> STARTING.POINT.TWO
textA
textB
textC
!terminate

Note that there can be any amount of strings between these two keywords. There can also be any string following the ">>". Finally, there can be an infinite amount of output arrays, based on the input text. Any help will be very appreciated. :)

Adam
  • 77
  • 7
  • If every array is like this, it is very easy: `For x As Integer = 1 To StringArray.Length - 2` – muffi Jun 14 '17 at 04:56
  • You can read the string and `.Split()` it by using `!terminate` keyword. But again that depends on how much bigger your string is and how much memory it will consume. If it is in file then better way is start reading file and in loop check for `!terminate` keyword and create new array element after each encounter of `!terminate` keyword. For `Split()` you can refer [here](https://stackoverflow.com/questions/2245442/c-sharp-split-a-string-by-another-string) – Mahesh Jun 14 '17 at 05:03
  • And also it is not clear if you have text in file or you have string with `newline` after each line. Can you make that clear. – Mahesh Jun 14 '17 at 05:07
  • Yes, it is stored in a file but passed as a parameter (in String() form, generated from File.ReadAllLines) to this function. – Adam Jun 14 '17 at 05:14

1 Answers1

1

Firstly, Split the entire file using vbCRLF as a delimiter (assuming the lines are terminated in this manner).

Secondly, start adding to the first array until you reach your '!terminate' string.

Repeat, adding to your second array.

So on, so on.

Mike
  • 419
  • 4
  • 11
  • Assuming if this is file `ReadLine()` will dot the job so no need to `Split()` using `vbCRLF` as a delimiter OP can just loop through till encounters the `!terminate` keyword and if its string still no need to `Split()` by new line he can just `Split()` using `!terminate` keyword. – Mahesh Jun 14 '17 at 05:05
  • @CoderofCode his example wanted 2 arrays, each array containing the elements, including the '!terminate'. While your method is valid, he still needs to separate the entries. Splitting with the '!terminate' would still require further splitting to get the elements into his arrays. 1 split is better than 2. – Mike Jun 14 '17 at 05:09
  • I could try this, but I have an unknown amount of arrays. 2 is not hard coded, I may have multiple, it all depends on the source file. – Adam Jun 14 '17 at 05:16
  • 1
    Make a global `LIst(Of List(Of String))`, `Dim` a `New List(Of String)` for each record, add that 'record' to the global list. – Mike Jun 14 '17 at 05:19