Split result of WebClient.DownloadString into multiple lines

937 Views Asked by At

I am trying to get a line foreach line in a webclient.DownloadString("pastebinsite"); but it says cannot convert type 'char' to 'string', so I add a string[] downloaded = wc.DownloadString(arac[0] + arac[1] + @"//" + arac[2] + "/raw/" + arac[3]);

that does not work because it says cannot convert type 'string' to 'string[]' I am stuck and cannot find a answer online for this.

I have tried changing types

        {
            StringBuilder sb = new StringBuilder();
            Console.WriteLine("start?");
            Console.ReadKey();
            string[] lines = File.ReadAllLines(Directory.GetCurrentDirectory() + @"\Lines.txt");
            WebClient wc = new WebClient();
            int _checked = 0;
            int _error = 0;
            foreach(string line in lines)
            {
                ++_checked;
                //Pastebin text viewer
                try
                {
                    if (line.Contains("pastebin"))
                    {
                        var arac = line.Split('/');

//ERROR LINE CANNOT CONVERT TYPE 'STRING' TO 'STRING[]' Below
                        string[] downloaded = wc.DownloadString(arac[0] + arac[1] + @"//" + arac[2] + "/raw/" + arac[3]);

                        foreach(string line2 in downloaded)
                        {
                            if (line2.Contains(":")
                                {

                                //Console.WriteLine(arac[0] + arac[1] + @"//" + arac[2] + "/raw/" + arac[3]);
                                Console.WriteLine(arac[0] + arac[1] + @"//" + arac[2] + "/raw/" + arac[3]);
                                sb.Append(downloaded);
                            }
                        }
                    }
                    else
                    {
                        //Console.WriteLine("Not valid pastebin link!");
                    }
                    Console.Title = "Checked : " + _checked;
                }
                catch(WebException ex)
                {
                    ++_error;
                    Console.WriteLine("Error: " + _error);
                }

            }
            File.WriteAllText(Directory.GetCurrentDirectory() + @"\Output " + _checked + ".txt", sb.ToString());
            Console.Clear();
            Console.WriteLine("FINISHED");
            Console.ReadKey();
        }```
1

There are 1 best solutions below

0
Oded Dahari On

wc.DownloadString(..) returns a string and not a string[].

you need to split the string in order to get a string[]
possible solution if you need that the string[] will contain lines would be:

var stringResult = wc.DownloadString(arac[0] + arac[1] + @"//" + arac[2] + "/raw/" + arac[3]);

then one of the following:

var lines = stringResult.Split(new [] { '\r', '\n' });
var lines = Regex.Split(stringResult, "\r\n|\r|\n");
var lines = stringResult.Split(new[] {"\r\n", "\r", "\n"}, StringSplitOptions.None)

and finally

foreach(string line in lines) {...}