为什么Console.ReadLine()忽略空格?

时间:2016-07-10 05:50:09

标签: c# console.readline

我正在尝试测试一个简单的控制台应用程序,它接受输入并检查它是否有唯一的字符。提供"的输入a",它只是将字符串作为" a"并忽略前面的空格。

你能帮我理解为什么会这样,以及如何让它接受空格作为字符串的一部分。

using System;
using System.Collections.Generic;

namespace CrackingTheCodingInterView
{
    public class CheckUniqueChars
    {
        public static void Main()
        {
            string inputString;

            bool checkUnique = false;

            Console.WriteLine("Enter string to check for unique chars: ");

            inputString = Console.ReadLine();

            checkUnique = UniqueChars(inputString);

            Console.WriteLine("String is: {0}", inputString);

            string output = checkUnique ? "has" : "does not have";

            Console.WriteLine("The input string {0} unique chars", output);

        }

        public static bool UniqueChars(string inputString)
        {
            List<char> uniqueCharsList = new List<char>();

            foreach(char c in inputString)
            {
                if(uniqueCharsList.Contains(c))
                {
                    return false;
                }
                else
                {
                    uniqueCharsList.Add(c);
                }
            }

            return true;
        }
    }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

这不是一个答案,但它的工作正常而不是忽略空格

enter image description here