【发布时间】:2015-06-11 20:49:52
【问题描述】:
我一直在查看其他关于 C# 中字数统计的类似问题的堆栈溢出文章,但对于我遇到的 pickle,没有任何帮助。
我有一个从文本文件输入文本的文本框。我按回车键在文本文件中创建一个新行,将文本分成三行。正文如下:
It's a test to see "if" the
application_for Top Image Systems
actually work. Hopefully it does work.
现在你可以看到应该有 17 个字,但是我的字数只有 15 个。经过一些试验和错误,我意识到问题一定是它在一个新行中。每次换行时,它都认为上一行的最后一个单词和新行的第一个单词一起作为一个单词(或者我认为程序是这样想的)。
我的问题是我下面的代码,我如何才能认识到如果有一个新行,它应该像空格一样分割单词?
下面是我的代码:
string nl = System.Environment.NewLine;
//Missing Code to read text file which I don't need to include in this example
do
{
textLine = textLine + txtReader.ReadLine();
}
//Read line until there is no more characters
while (txtReader.Peek() != -1);
//seperate certain characters in order to find words
char[] seperator = (" " + nl).ToCharArray();
//number of words
int numberOfWords = textLine.Split(seperator, StringSplitOptions.RemoveEmptyEntries).Length;
【问题讨论】: