0

I have a string that has many line breaks and I want to build a string array that has each line of the string in an array slot.
Like this: stringArray[4] should have line 5 of the string in it.

I don't get an error in this code the string-array seems to have something in slot [0] the rest of the array seems to be empty:

using System;

namespace ProjectWebClientClass
{
    class Program
    {
        static void Main(string[] args)
        {
            string htmlContent = String.Empty;

            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                //this method stores the html code in a string
                htmlContent = client.DownloadString("http://www.rheinwerk-verlag.de");
            }

            Console.WriteLine(htmlContent);

            string[] htmlLines = htmlContent.Split(Environment.NewLine);
            //the following output is empty:
            Console.WriteLine(htmlLines[4]);
        }
    }
}
  • 1
    That mean, that string doesn't have a character of `Environment.NewLine` – Fabio Jan 18 '20 at 07:43
  • https://yetanotherchris.dev/csharp/extracting-all-links-from-a-html-page/ –  Jan 18 '20 at 07:53
  • The code (which is the same as [duplicate](https://stackoverflow.com/questions/1547476/easiest-way-to-split-a-string-on-newlines-in-net) that answers your question *as asked*) should work if data matches what you describe ("string with new lines"). If you need help debugging your particular case - please review [MCVE] guidance on posting code and [edit] post to make sure that all necessary data is inline (in particular `htmlContent` should be inline string constant and not obtained from some other place) along with just enough code to demonstrate the problem (remove all HTTP stuff). – Alexei Levenkov Jan 18 '20 at 07:58
  • 1
    this code worked: string[] lines = htmlContent.Split( new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None ); – UnhandledException321 Jan 18 '20 at 08:23

0 Answers0