Search code examples
c#arraysstringwinformsstring-function

I need to get a string Array with single lines, which should be extraced from a big text with paragraphs


I got an idea that i wanted to make today but ran into the problem that i have a string variable with paragraph, but i need an Array of single lines. So I tried to do this with the String.Substring and the String.IndexOf functions but this only worked kinda because i dont exactly know how VisualStudio handels the Index of Paragraphs and how strings work with paragraphs because i just learned C# this year.

I tried it in Windows-Forms btw.

Can anyone tell me how index's with paragraphs work or especialy how to use them correctly.

This is the code i tried which only works for the 1st line and works kinda with the 2nd but not with any further

        string input_raw;
        string[] input = new string[100];
        int index_zeile = 0;
        int x = 0, y = 0;
        input_raw = input_text_box.Text;

        for (int i = 0; i < 100; i++)
        {
            if (i == 0)
            {
                y = input_raw.IndexOf(";");
                input[i] = input_raw.Substring(index_zeile, y);
                x = y + 1;
            }
            else
            {
                index_zeile = input_raw.IndexOf(";", x);
                input[i] = input_raw.Substring(x, index_zeile-3);
                x = x + index_zeile;
            }
        }

Solution

  • I wish you entered your text input, but you can split the string into an array. The following code assigns a multi-line string to an array.

    string[] lines = input_text_box.Text.Split('\n');