C # Read the multi-line input string from the console

Source: Internet
Author: User

Problem: You need to input multiple rows in the console, but want to accept them, rather than a limited row.

Solution:

 

  Static     String  Acceptmultiline ()
{
Consolekeyinfo cki;
Console. treatcontrolcasinput = True ; // Prevents Ctrl + C Replication
Console. writeline ( " Press the F10 key to quit: \ n " );
String Result = String . Empty;
Do
{
Cki = Console. readkey ();
Result + = Cki. keychar;
If (Cki. Key = Consolekey. Enter)
{
Result + = System. environment. newline; // If you press enter, a line feed sign is added.
}
} While (Cki. Key ! = Consolekey. F10 ); // Press F10 to exit
Return Result;
}

Public Static Void Main ( String [] ARGs)
{
String S = Acceptmultiline ();
Console. writeline (s );
Console. Readline ();
}

Thanks to Jeffery for reminding you:

ModifiedCode:

 

  Static     String  Acceptmultilinechars2 ()
{
Consolekeyinfo cki;
Console. treatcontrolcasinput = True ; // Prevents Ctrl + C Replication
Console. writeline ( " Press the CTRL + enter key to quit: \ n " );
String Result = String . Empty;
Do
{
Cki = Console. readkey ();
If (Cki. Key = Consolekey. Enter)
{
Result + = System. environment. newline; // If you press enter, a line feed sign is added.
Console. setcursorposition ( 0 , Console. cursortop + 1 ); // Move the cursor down a row
}
Result + = Cki. keychar;
} While (Cki. Key ! = Consolekey. Enter | (Cki. Modifiers & Lelemodifiers. Control) = 0 ); // Press Ctrl + enter to exit
Return Result;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.