如何在 C# 中将输入读取为字符串?

csharpprogrammingserver side programming更新于 2025/7/12 10:52:17

要在 C# 中将输入读取为字符串,请使用 Console.ReadLine() 方法。

str = Console.ReadLine();

以上代码将输入读取为字符串。由于输入默认采用字符串,因此此处无需使用 Convert 方法。

现在显示用户输入的字符串 −

示例

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      string myStr;

      // 使用 ReadLine() 读取输入的行
      myStr = Console.ReadLine();

      // 显示该行
      Console.WriteLine("Result = {0}", myStr);
   }
}

输出

Result =

以下是输出。假设用户输入的是"Amit" −

Result = amit

相关文章