Reputation: 75
private async void Button_Click(object sender, RoutedEventArgs e)
{
int word = 1;
string FileName;
openFileDialog1.Filter = "txt files (*.txt)|*.txt";
openFileDialog1.FilterIndex = 2;
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ShowDialog();
FileName = openFileDialog1.FileName;
FileStream stream = File.Open(FileName, FileMode.Open);
await Task.Run(() =>
{
using (StreamReader reader = new StreamReader(FileName))
{
string content = reader.ReadToEnd();
}
});
}
As you can see, I decided to create a "content" string that would contain all the text inside of a .txt file. How do I now select a random word from this string that is not an array?
Upvotes: 1
Views: 924
Reputation: 26
You should split on the spaces and then use random to get a random integer.
Random random = new Random();
string[] split = content.Split(" ");
string randomString = split[random.Next(0,split.length)];
Upvotes: 1