Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我能';你逃不过问号吗?_C# - Fatal编程技术网

C# 为什么我能';你逃不过问号吗?

C# 为什么我能';你逃不过问号吗?,c#,C#,我需要计算每个单词在从文件读取的文本中出现的次数。问题是我必须避开一些常见的符号,我正在这样做。除了问号“?”之外,所有这些都成功地去除了,我仍然不知道为什么会发生这种情况。我指的是代码。再次感谢 namespace DictionariesHashTablesAndSets { using System; using System.Collections.Generic; using System.IO; class WordsOccurencesInText { static vo

我需要计算每个单词在从文件读取的文本中出现的次数。问题是我必须避开一些常见的符号,我正在这样做。除了问号“?”之外,所有这些都成功地去除了,我仍然不知道为什么会发生这种情况。我指的是代码。再次感谢

namespace DictionariesHashTablesAndSets
{
using System;
using System.Collections.Generic;
using System.IO;

class WordsOccurencesInText
{
    static void Main()
    {
        StreamReader reader = new StreamReader("../../text.txt");
        string textFromFile = reader.ReadToEnd();

        string[] words = SplitWords(textFromFile);

        for (int index = 0; index <= words.Length - 1; index++)
        {
            words[index] = words[index].ToLower();
        }

        IDictionary<string, int> dict = new Dictionary<string, int>();

        foreach (var word in words)
        {
            int count = 1;
            if (dict.ContainsKey(word))
            {
                count = dict[word] + 1;
            }

            dict[word] = count;
        }

        Console.WriteLine(textFromFile);

        foreach (var word in dict)
        {
            Console.WriteLine("{0} -> {1} times", word.Key, word.Value);
        }

    }

    private static string[] SplitWords(string textFromFile)
    {
        char[] separators = new char[] { '.', ',', ' ', '?', '!', ';', '-' };
        string[] words = textFromFile.Split(separators, StringSplitOptions.RemoveEmptyEntries);

        return words;
    }
}
}
文本文件的示例:

这里只是一些随机的文本,文本,文本,还有更多的随机-随机文本?这是文本。文本,文本,文本这个文本!这是课文吗


控制台写入了
,因为它是一个无法显示的字符。该字符位于文件中这些部分之间:

just Some random text over Here, TEXT, text, and more random - random text Text? This is the TEXT. Text, text, text 

你看到开头的空格了吗?它不是普通的空间,也不是第一部分的空间。然而,它的ASCII值是150。(普通空间的ASCII值为32) 如果删除此异常空间,
将消失

如果要检查,请参加此测试:

just Some random text over Here, TEXT, text, and more random - random text Text? This is the TEXT. Text, text, text THIS TEXT! Is this the text?

这并不重要。你应该包含输入的
textFromFile
字符串,这样你的问题就可以在没有猜测的情况下重现。你能发布文本文件吗?请给我们展示一个textOkey示例,我参考了该文件,你可以再次检查主题@DeeMacI我刚刚开始想为什么我从文件中删除
,我仍然在输出中接收到该信息:D@StanimirYakimov我开始怀疑,因为它显示了什么?在错误的地方。你很接近:D。
 THIS TEXT! Is this the text?
just Some random text over Here, TEXT, text, and more random - random text Text? This is the TEXT. Text, text, text THIS TEXT! Is this the text?