Console.ReadLine()字符数 [英] Console.ReadLine() number of characters

查看:98
本文介绍了Console.ReadLine()字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该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.ReadConsole.ReadLine完全执行它们的操作,而没有其他操作.

控制台的工作风格根本没有意义.该控制台旨在与通常可以清晰地感知和输入行的操作员一起工作,在最坏的情况下,还可以输入一些适当分隔的单词.

从理论上讲,您可以获取输入流System.Console.In并以字节对字节的方式对其进行操作,请参见 Enumeration-基于命令行的实用程序 [ ^ ].

—SA
No, you cannot. The Console methods Code.Read and Console.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 stream System.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 use Console.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 use Console.ReadLine or Console.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆