How can I limit the number of characters for a console input? C#

后端 未结 2 1394
我在风中等你
我在风中等你 2020-12-22 06:06

Basically I want 200 characters maximum to come up in Console.ReadLine() for user input before characters start being suppressed. I want it like TextBox.MaxLength except for

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-22 06:53

    There is no way to limit the text entered into ReadLine. As the MSDN article explains,

    A line is defined as a sequence of characters followed by a carriage return (hexadecimal 0x000d), a line feed (hexadecimal 0x000a), or the value of the Environment.NewLine

    What you can do, is use ReadKey in a loop that does not allow going over 200, and breaks if the user keys Environment.NewLine.

提交回复
热议问题