I am trying to load text into memory from different text files. They are all words, and they are all grouped by the length of the word in their respective text files (e.g. words3.txt, words4.txt…)
I am using StreamReader to work with the files, and because of the syntax I am fairly certain I can iterate which file it’s working with if I do it inside of a for loop. I don’t see why I should have 12 different using statements.
String[] words3 = new String[2000];
for (int i = 0; i < 12; i++)
{
using (StreamReader sr = new StreamReader(path + "words" + (i+3) + ".txt"))
{
String strTemp = sr.ReadLine();
words3 = strTemp.Split(' '); //My current logic fails here
}
}
I wanted to iterate through my different words arrays (words3, words4… words15) but naturally I run into an issue with the name of the array that I’m storing these in. It remains unchanged and so I would simply be overwriting it 12 times. In VB.NET I could concatenate an iterator variable to the array name like so (or something closely resembling this):
words & (i+3) = strTemp.Split(' ');
This obviously won’t work in C# the way I’ve described it. What is the best approach to solving this problem? Can I put the arrays into a larger array and iterate through them somehow? In the text files the words are not stored on individual lines, they are separated by a single space. To save time, when I go to see if a user’s word is contained in my “dictionary”, I only want to search for a match in the array that houses words with the appropriate number of letters.
Use something like a dictionary:
Then to get the words back: