我基本上只是想在字符串中选择一个随机值,并用“ |”分隔。我找不到一个好的例子,有人有想法吗?

string[] mystrings = ("apple|orange|mayo|fruit|dog"):

string blah = "here i am "+resultsofrandom+" result chosen from mystring was " resultofrandom


显然,字符串等等只是一个例子,我只想从mystrings中随机选择一个字符串,然后重新转换为新字符串...

最佳答案

string[] mystrings = "apple|orange|mayo|fruit|dog".Split('|');
Random rnd = new Random();
string blah1 = mystrings[rnd.Next(mystrings.Length)];
string blah2 = mystrings[rnd.Next(mystrings.Length)];
string sentence = "here i am " + blah1 + " result chosen from mystring was " + blah2

关于c# - C#选择随机字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3970342/

10-11 18:06