如何检查标准输入是否为空?

时间:2013-02-11 07:12:05

标签: c# console

我需要知道标准输入中是否有任何输入符号。

但我不知道如何检查这种情况。

据我所知,我不能使用Console.Read(),因为它实际上会读取下一个输入符号。

7 个答案:

答案 0 :(得分:3)

我认为您可以将Console.In用作System.IO.TextReader并使用Peek() - 方法:

if(Console.In.Peek() == "I don't know what it will be...")
{ /* Do something */ }

答案 1 :(得分:2)

console.writeline();
var a = console.readline();
if(a == null)
{
do something..
}
else
{
do something..
}

答案 2 :(得分:2)

if(Console.In.Peek()!=-1) //solves the problem

答案 3 :(得分:1)

如果您想检查控制台标准输入上是否有任何数据没有实际读取,我建议您选择以下内容:

using (var sr = new StreamReader(Console.OpenStandardInput()))
{
    //check if there are any characters on the input stream
    if(!sr.EndOfStream)
    {
        //do whatever You want to do when the stream is not empty
    }
    else
    {
        //do whatever You want to do when the stream is empty
    }
}

答案 4 :(得分:0)

我希望我理解你的问题,你可以尝试这样的事情:

var userInput = Console.ReadLine();
if (string.IsNullOrEmpty(userInput))
{
 // do stuff here
}

答案 5 :(得分:0)

也许这会有所帮助:

 string s = Console.ReadLine();
   if (s.Contains('@') Or s.Contains('!')) // you can add other symobols as need
   {
   //do your work

   }

答案 6 :(得分:0)

如果您要查看用户插入的输入类型,那么如果符号指的是“<,>,?,/,@,则可以使用此类输入,#,$,%,^,&安培;等...“ 这些链接可以帮助您Validation of input fieldsthis one

  

请正确构建您的问题很难理解是什么   你问的是