Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multi line textbox to array C#

I am trying to transfer values from each line of a multi line text box into either a string array or a multidimensional array. I also have 3 multi-line text boxes which need to put into the same array. Below is one of the methods I have been trying:

ParkingTimes[0] = tbxtimeLimitS1.Text;

for (int i = 1; i <= 10; i++)
   ParkingTimes[i] = tbxparkingTimesS1.Lines;

ParkingTimes[11] = tbxtimeLimitS2.Lines;

for (int x = 0; x <= 10; x++)
   for (int i = 12; i <= 21; i++)
       ParkingTimes[i] = tbxparkingTimesS2.Lines;

ParkingTimes[11] = tbxtimeLimitS2.Lines[0];

for (int x = 0; x <= 10; x++)
    for (int i = 23; i <= 32; i++)
        ParkingTimes[i] = tbxparkingTimesS3.Lines;

What am I doing wrong? Is there a better way to accomplish this?

like image 674
Jack TcRebel Treble Avatar asked Nov 30 '22 02:11

Jack TcRebel Treble


1 Answers

You can simply do

string[] allLines = textbox.Text.Split('\n');

This will split each line and store the results in the appropriate index in the array. You can then iterate over them like so:

foreach (string text in allLines)
{
    //do whatever with text
}
like image 138
Bryan Crosby Avatar answered Dec 05 '22 16:12

Bryan Crosby