computhomas computhomas - 3 months ago 21x Question

Splitting text into 2d array

I am trying to split a block of text into a 2d array. I know how to split a line into an array of words, and I know how to split multiple lines into an array of lines, but I would like to do both simultaneously.

For example, I want this :

Tim is nice

Jim is mean

would become


So Far, I have been able to use

Str1.Split(new String() {Environment.NewLine}, StringSplitOptions.None)

To make the example string into:

(Tim is nice,Jim is mean)

And I've used
to convert
"Tim is nice"


Also, if any of my code is bad syntax or anything like that let me know, I'm a Python programmer primarily.


This isnt a 2D array:


That is a jagged array or an array of arrays. Since each line/sentence may not have the same number of words, it is a better choice. For your example, you will have an array of 2 to hold each "line", then each of those will hold an array of however many words there are.

Dim original = "Tim is nice" & Environment.NewLine & "Jim is very mean"

Dim lines = original.Split(New String() {Environment.NewLine.ToString},

Dim results(lines.Count - 1)() As String
For n As Int32 = 0 To lines.Count - 1
    ' store array of words to results(n)
    results(n) = lines(n).Split(" "c)
' glue a line back together and show it
Console.WriteLine(String.Join(" ", results(1)))

results(0) will hold the Tim words, results(1) will hold the Jim words. Results (of the writeline):

Jim is very mean