c# - 从输入中计算字符时,在输出中获取空格字符

标签 c# collections console character

我已经使用它几天了,我正在尝试完成一个控制台应用程序,系统会提示我们输入一个我们自己的字符串,输出会列出每个唯一字符并在后面加上一个计数它。在结果的末尾,会显示一个计数,显示找到了多少个独特的字符。无论是否为大写,所有内容都被翻译为小写。他们的关键是使用集合。这是我到目前为止所拥有的。尽管我使用 if 语句来捕获它们,但我的输出在结果中显示了两个空格字符。谁能指出一个我忽略的概念?

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

public class LetterCountTest
{
    public static void Main(string[] args)
    {
        // create sorted dictionary based on user input
        SortedDictionary<string, int> dictionary = CollectLetters();

        // display sorted dictionary content
        DisplayDictionary(dictionary);
    } // end Main

    // create sorted dictionary from user input
    private static SortedDictionary<string, int> CollectLetters()
    {
        // create a new sorted dictionary
        SortedDictionary<string, int> dictionary =
           new SortedDictionary<string, int>();

        Console.WriteLine("Enter a string: "); // prompt for user input
        string input = Console.ReadLine(); // get input

        // split every individual letter for the count
        string[] letters = Regex.Split(input, "");

        // processing input letters
        foreach (var letter in letters)
        {
            string letterKey = letter.ToLower(); // get letter in lowercase
            if (letterKey != " ") // statement to exclude whitespace count
            {
                // if the dictionary contains the letter
                if (dictionary.ContainsKey(letterKey))
                {
                    ++dictionary[letterKey];
                } // end if
                else
                    // add new letter with a count of 1 to the dictionary
                    dictionary.Add(letterKey, 1);
            }
        } // end foreach

        return dictionary;
    } // end method CollectLetters

    // display dictionary content
    private static void DisplayDictionary<K, V>(
       SortedDictionary<K, V> dictionary)
    {
        Console.WriteLine("\nSorted dictionary contains:\n{0,-12}{1,-12}",
           "Key:", "Value:");

        // generate output for each key in the sorted dictionary
        // by iterating through the Keys property with a foreach statement
        foreach (K key in dictionary.Keys)
            Console.WriteLine("{0,-12}{1,-12}", key, dictionary[key]);

        Console.WriteLine("\nsize: {0}", dictionary.Count);
        Console.ReadLine();
    } // end method DisplayDictionary
} // end class LetterCountTest

我的输出表明我使用了字母表中的每个字母,但在“a”上方还有一个空格和它的两个实例。我不知道这是从哪里来的,但我猜它是在计算空字符或回车符。我使用的字符串是...

敏捷的棕色狐狸跳过懒惰的狗

除了每个字母都数一次外,它还数 e 三次,h 两次,o 四次,r 两次,t 两次,u 两次。

最佳答案

你遇到的问题在Regex.Split()的行为。摘自 the msdn page on the method ...

If a match is found at the beginning or the end of the input string, an empty string is included at the beginning or the end of the returned array.

这正是您调用 Regex.Split(input, ""); 时发生的情况.为了解决这个问题,您可以从代码中删除正则表达式匹配,将字典的所有实例更改为 SortedDictionary<char, int>。 ,并使用 foreach而是循环遍历输入字符串的每个字符。

foreach (char letter in input.ToLower())
{
    if (!Char.IsWhiteSpace(letter))
    {
         //Do your stuff
    }
}

关于c# - 从输入中计算字符时,在输出中获取空格字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23046240/

相关文章:

c# - 控制台的调整大小事件

c# - Linq - 每个组的最高值

c# - 在 C# 中从 Window 服务启动应用程序

c# - .NET Web API + session 超时

java - 为什么没有针对 ConcurrentHashMap 的 ConcurrentHashSet

android - Xamarin Android - 关闭 Mono 日志

c# - 删除没有子节点的父节点

java - 如何从哈希表中删除这些值?

java - 按字段的自定义顺序对对象列表进行排序

python - 任何语言中最流畅的 REPL 控制台