am trying to read random words from a textfile . well have managed to read the words and display them in textblock problem is i only need to read words of certain
length only for example 5 to ten letters only
here is my code so far
private void Mywords()
{
var ResourceStream = Application.GetResourceStream(new Uri("TextFile1.txt", UriKind.Relative));
using (Stream Myfilestream = ResourceStream.Stream)
{
string s = "";
StreamReader myStreamReader = new StreamReader(Myfilestream);
s = myStreamReader.ReadToEnd();
s = s.Trim(); //tlbRandomWords.Text = s; // char[] delimiters = { '\n', '\r' };
string[] words = s.Split(); // int index = rand.Next(0,delimiters.Count());
if (s.Split().Length < 6)
{
tlbRandomWords.Text = words[rand.Next(words.Count())].ToUpper();
}
}
}
{
var ResourceStream = Application.GetResourceStream(new Uri("TextFile1.txt", UriKind.Relative));
using (Stream Myfilestream = ResourceStream.Stream)
{
string s = "";
StreamReader myStreamReader = new StreamReader(Myfilestream);
s = myStreamReader.ReadToEnd();
s = s.Trim(); //tlbRandomWords.Text = s; // char[] delimiters = { '\n', '\r' };
string[] words = s.Split(); // int index = rand.Next(0,delimiters.Count());
if (s.Split().Length < 6)
{
tlbRandomWords.Text = words[rand.Next(words.Count())].ToUpper();
}
}
}
VulpesPosted Jan 26, 2014, 12:03 PM
VulpesPosted Jan 26, 2014, 12:59 PM
\b\w{5,10}\b is a regular expression which matches words between 5 and 10 characters long.
\b and \w are what are called 'metacharacters' in regular expression terminology.
\w matches any of the following: a -z, A - Z or 0-9 and also the underscore _ character. {5,10} means between 5 and 10 consecutive characters of those types.
\b means a word boundary: generally a space, punctuation mark or the beginning or end of the line or string. However, the actual boundary character (if there is one) isn't included in the overall match.
When you put this regular expression in a string, it's easier to use a 'verbatim' string (prefixed by @) so the backslashes aren't treated as normal C# escape characters (\n, \r, \t etc).
This line then finds all the matches in the text:
MatchCollection words = Regex.Matches(s, regExp);
You can then use the indexer syntax, [], to select a Match object at random from this collection. When you do that, you need to use the Match object's Value property to get the actual word.
@Sunil
Well, this line:
if (words[i].Length > 5 && words[i].Length < 5)
should be:
if (words[i].Length >= 5 && words[i].Length < = 10)
However, a more fundamental problem is that you need to filter these words out before you can choose one at random.
Another approach would to keep on choosing words at random until you get one that's between 5 and 10 characters. However, the code for this is rather messy compared to the Regex approach.
SUNIL GUTTAPosted Jan 26, 2014, 12:54 PM
solomon mwikaPosted Jan 26, 2014, 12:52 PM
SUNIL GUTTAPosted Jan 26, 2014, 12:38 PM
solomon mwikaPosted Jan 26, 2014, 12:30 PM
and by the way as a bonus it worked well but really need a little under standing
SUNIL GUTTAPosted Jan 26, 2014, 7:31 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace countwords
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader("C:\\test.txt");
string str= sr.ReadToEnd();
sr.Close();
string[] words = str.Split();
for (int i = 0; i < words.Length ; i++)
{
if (words[i].Length > 5 && words[i].Length < 5)
{
Console.WriteLine(words[i]);
}
}
Console.ReadKey();
}
}
}
--------------------------------------If this solves make it rated-------------------------