C# Read random line from URL -


skip tldr version if aren't explanation of logical processing.

i've been fiddling around program following: on button click, reads random line locally stored text document, without ability repeat itself

however wan't able read url, not locally stored solution.

so following code current function , tried, , resulted with.

string[] readtext = file.readalllines(@"path\file.txt"); random rnd = new random(); textbox1.text=(readtext[rnd.next(readtext.length)]); 

naturally read file stored in path section, creates random generator , makes textbox' output random line entire document.

what tried url, , partially worked..

webclient webcon= new webclient(); string webdata = webcon.downloadstring("url"); textbox1.text = webdata; 

followed following generate random line of document:

random rnd = new random(); textbox1.text = ((webdata[rnd.next(webdata.length-1)])); 

however invalid , had convert char string following way, resulted in funny , utterly useless textbox.

random rnd = new random(); textbox1.text = char.tostring((webdata[rnd.next(webdata.length-1)])); 

tl;dr version

i have program reads local file following method:

string[] readtext = file.readalllines(path) 

and generates random line said document display in textbox so:

random rndm = new random();             textbox1.text=(readtext[rndm.next(readtext.length)]); 

however want able do, read url (online document). tried completing task webclient method resulted in needing convert char string on textbox url.

webclient webcon= new webclient(); string webdata = webcon.downloadstring("url"); textbox1.text = webdata; 

i hope question isn't close enough reposte possible, did ensure check thoroughly first on relevant threads , couldn't complete task. thank in advance!

how using string.split() method? boil down this:

//\n denotes newline caracter var lines = webdata.split('\n'); 

you can proceed lines array did result you've obtained file.

i'd recommend use webclient this:

using (var webcon = new webclient())  {     string webdata = webcon.downloadstring("url");     //process webdata } 

the using {} part makes sure webclient correctly disposed (which frees resources).

some more explanation thought process:
strings arrays composed of chars, if access element inside, i'll of type char:

string myval = "hi mom"; char mychar = myval[0]; 

the random object should constructed once in application, make sure if generate lot of random numbers in short time reuse same random object (or might receive multiple times same values)

full example:

private static random _random = new random(); static void main(string[] args) {     using (var webcon = new webclient())     {         var webdata = webcon.downloadstring("http://stackoverflow.com/questions/43253136/");         var lines = webdata.split('\n');         var myrandomline = lines[_random.next(0, lines.length - 1)];         mytextblock.text = myrandomline;     } } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -