-1

C# .NET Framework 4.7.2

So I've got a link: https://pastebin.com/raw/fZzAdRJw and I want to get the text of the third line. How do I do that?

My code:

private void ReadLine()
{
    using (var wc=new System.Net.WebClient())
    {
        wc.DownloadString("https://pastebin.com/raw/fZzAdRJw");
    }
}

I didn't provide a lot of the code because I totally don't know how to do this. Sorry for the noobiness, I'm totally bad at C#.

Axyclez
  • 15
  • 1
  • 9
  • Have you tried [splitting](https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=net-5.0) the data on `\n`? So something like: `var thirdString = wc.DownloadString("https://pastebin.com/raw/fZzAdRJw").Split('\n')[2];` The split returns a string array, and using `[2]` would access the 3rd item, arrays are 0 index based. Also this is just a simple example. TBH I would download the string, try and split it out and then see if that index exists before trying to access the string by that index. – Trevor Feb 23 '21 at 19:13
  • Adding `.Split(new [] {"\r\n"}, StringSplitOptions.None)[2]` after the parenthesis works for me. – evilmandarine Feb 23 '21 at 19:16
  • 1
    Does this answer your question? [Easiest way to split a string on newlines in .NET?](https://stackoverflow.com/questions/1547476/easiest-way-to-split-a-string-on-newlines-in-net) – Trevor Feb 23 '21 at 19:19
  • @Codexer `Does this answer your question? Easiest way to split a string on newlines in .NET?` no it doesnt but Ill try evelmandarine and your comments to see if it works – Axyclez Feb 23 '21 at 19:26
  • @Codexer thanks your comment works and I like it because its short and simple – Axyclez Feb 24 '21 at 11:38

1 Answers1

0
static void Main(string[] args)
        {
            WebClient web = new WebClient();
            System.IO.Stream stream = web.OpenRead("https://pastebin.com/raw/fZzAdRJw");
            String text;
            using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
            {
                text = reader.ReadToEnd();
            }
            string[] lines = text.Split(
                new[] { Environment.NewLine },
                StringSplitOptions.None
            );
            Console.WriteLine(lines[2]);
        }
aetrnm
  • 402
  • 3
  • 13
  • this one definitely does – aetrnm Feb 23 '21 at 19:32
  • You should be disposing `web` and `stream` when you're done with them; TBH wrap them in `using` statements would be a good idea. – Trevor Feb 23 '21 at 19:37
  • @Codexer tbh I haven't written this code, it already exists on the web, every question has an answer, I just copied it, tested it and saw that it worked fine, but I am a beginner at programming, so don't even what the hell is happening above – aetrnm Feb 23 '21 at 19:43
  • 1
    @aetrnm we all start somewhere, it's ok, just a suggestion is all. – Trevor Feb 23 '21 at 19:49