i basically just want to pick a random value in a string split by ‘|’. I can’t find a good example does anybody have an idea?
string mystrings = ("apple|orange|mayo|fruit|dog"):
string blah = "here i am "+resultsofrandom+" result chosen from mystring was " resultofrandom
obviously string blah is just an example, i just want the random chosen string from mystrings back into a new string…
,
string mystrings = "apple|orange|mayo|fruit|dog".Split('|');
Random rnd = new Random();
string blah1 = mystringsrnd.Next(mystrings.Length);
string blah2 = mystringsrnd.Next(mystrings.Length);
string sentence = "here i am " + blah1 + " result chosen from mystring was " + blah2
,
You could do this rather simply by splitting the string:
string mystrings = "apple|orange|mayo|fruit|dog".Split('|');
Then use a the Random
class to pick one of those strings:
int choice = new Random().Next(mystrings.Length);
Now you can put it together:
string blah = "Your selection is: " + mystringschoice;
,
Random rnd= new Random();
int baseZeroArrayLen = 0;
string mystrings = ("apple|orange|mayo|fruit|dog").Split('|');
baseZeroArrayLen = mystrings.Length - 1;
int randomNumber = rnd.Next(baseZeroArrayLen);
string rndString = mystringsrandomNumber;
,
var mystrings = ("apple|orange|mayo|fruit|dog").Split('|');
string blah = "here i am " + mystringsnew Random().Next(0, mystrings.Length) + " result chosen..";
I think it will work as expected
,
This should do it:
string mystrings = ("apple|orange|mayo|fruit|dog").Split('|');
Random randomInt = new Random();
string blah = mystringsrandomInt.Next(mystrings.Length);
,
Use String.Split()
to split up the delimited string and store each separate value in a string array. Then randomly pick an index into that array and display the corresponding string.
,
Completely unnecessary LINQ alternative. Although the string.Format might be nice here.
string mystrings = "apple|orange|mayo|fruit|dog".Split('|');
string blah = string.Format("here i am {0} result chosen from mystring was {0}",
mystrings.Skip(new Random().Next(mystrings.Length)).First());