There is a much better way of doing this: string.Split()
: if you read the entire string in, C# can automatically split it on every space:
string[] words = reader.ReadToEnd().Split(' ');
The words
array now contains all of the words in the file and you can do whatever you want with them.
Additionally, you may want to investigate the File.ReadAllText
method in the System.IO
namespace - it may make your life much easier for file imports to text.
Edit: I guess this assumes that your file is not abhorrently large; as long as the entire thing can be reasonably read into memory, this will work most easily. If you have gigabytes of data to read in, you'll probably want to shy away from this. I'd suggest using this approach though, if possible: it makes better use of the framework that you have at your disposal.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…