adrotter
adrotter

Reputation: 311

C# Console, Checking when just enter is entered

I have a simple if statement

string response = Console.Readline();
if ((response[0] >= 97 || response[0] <= 122) && response.Length == 1 && Char.IsLetter(response[0]) && response[0] != 13)

I'm looking for input that is between A-Z, and only 1 character long. Whenever I type just enter into the console, this if statement executes (which is why I thought response[0] != 13 would prevent that).

So using Console.ReadLine(), how can I stop the if statement from executing if just the enter key is used in the console?

Upvotes: 0

Views: 64

Answers (2)

if you just press enter, "response" will be an empty string

to exclude the empty string from your if statement, type

if(response != "" && ...

Upvotes: 1

Jesse Williams
Jesse Williams

Reputation: 662

If I'm understanding, you want to read in as soon as the letter character is pressed and don't want anything to happen when Enter is pressed. If so, use Console.ReadKey() instead:

https://msdn.microsoft.com/en-us/library/471w8d85(v=vs.110).aspx

Upvotes: 1

Related Questions