Console.ReadLine()字符数 [英] Console.ReadLine() number of characters
问题描述
该Console.ReadLine()如何仅获取给定数量的字符?例如20th
我读过给您起的名字,但不能超过20个字符,输入时必须动态检查.
由于我在[(这里有20个空格)]之间设置了文本类型,因此我不能让您键入以]"形式转义的文本.
How to do that Console.ReadLine() fetch only a given number of characters? For example, 20th
I have read that gives you the name but it can not be longer than 20 characters and this must be checked dynamically as you enter.
Since I have a set type text between [(here 20 space here)] and I can not let you type text escaped as "]".
Console.SetCursorPosition(25, 5);
Console.WriteLine("Enter the playlist name");
Console.SetCursorPosition(25, 6);
Console.WriteLine("[");
Console.SetCursorPosition(47, 6);
Console.WriteLine("]");
Console.SetCursorPosition(26, 6);
Console.ForegroundColor = ConsoleColor.White;
// ****************
// *****************
MyPlayList.Add(new PlayList(Console.ReadLine()));
现在我的问题是如何限制Console.Readline()仅接受XX(假设为20)个字符,并且不允许用户在控制台屏幕上进一步输入事件类型?
Now my question How can I restrict the Console.Readline() to accept only XX(let say 20) number of characters and not allow user to event type on console screen further ?
推荐答案
否,您不能.Console
方法Code.Read
和Console.ReadLine
完全执行它们的操作,而没有其他操作.
控制台的工作风格根本没有意义.该控制台旨在与通常可以清晰地感知和输入行的操作员一起工作,在最坏的情况下,还可以输入一些适当分隔的单词.
从理论上讲,您可以获取输入流System.Console.In
并以字节对字节的方式对其进行操作,请参见 ^ ].
但是,即使那样也没有任何意义.有意义的是以下模式:始终使用Console.ReadLine
,读取整个字符串,将其分配给局部变量,然后以所需的方式解析此字符串.保持对用户的简单和方便.
另外,让我告诉您:从任何实际意义上讲,在控制台中与用户进行对话都是不可靠的.很难处理用户的错误等.此类对话框通常仅用于软件工程专业学生的学习目的.如果您需要更复杂的输入,请开发一个带有窗口/窗体和控件的窗口化应用程序.
在99.9%的情况下,可用的应用程序根本不使用Console.ReadLine
或Console.Read
.它只是希望在运行时在命令行中一次解析所有信息.如果期望用户提供更多数据,则使用输入文件.
对于命令行解析,欢迎使用我易于使用的库 Enumeration-基于命令行的实用程序 [ ^ ].
—SA
No, you cannot. TheConsole
methodsCode.Read
andConsole.ReadLine
do exactly what they do and nothing else.
The style of work with console simply makes no sense. The console is designed to work with a human operator who normally can clearly perceive and input lines, in worst case, some properly delimited words.
Theoretically speaking, you can get an input streamSystem.Console.In
and operate it in byte-to-byte manner, see http://msdn.microsoft.com/en-us/library/system.console.in.aspx[^].
However, even that would make no sense. What makes sense is the following pattern: always useConsole.ReadLine
, read the whole string, assign it to the local variable and parse this string the way you want. Keep it simple and convenient to the user.
Also, let me tell you: in any practical sense, a dialog with the user in a console is unreliable; it''s hard to process user''s error, etc. Such dialogs are often used just for learning purposes by software engineering students. If you need more complex input, develop a windowed application, with a Window/Form and controls.
The usable application in 99.9% cases don''t useConsole.ReadLine
orConsole.Read
at all. It simply expect all the information at once in a command line which is parsed during run time. If more data is expected from a user, an input file is used.
For command-line parsing, you are welcome to use my easy-to-use library Enumeration-based Command Line Utility[^].
—SA
您不能. ReadLine方法始终返回用户输入的内容,直到用户键入指示行尾或流尾的字符,您才获得任何控制.您无法告诉它可以获取多少个字符.
为此,您需要调用 ReadKey方法 [
You can''t. The ReadLine Method always returns what the user input, you do not get any control until the user types a character indicating the end of the line, or end of the stream. You cannot tell it how many character it may fetch.
To do this, you need to call the ReadKey method[^] in a loop, and check for end-of-line yourself.
这篇关于Console.ReadLine()字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!