如何在 C# 中將輸入讀作字串?


要將輸入作為字串在 C# 中讀取,請使用 Console.ReadLine() 方法。

str = Console.ReadLine();

上述程式碼將以字串形式讀取輸入。你不需要使用 Convert 方法,因為輸入預設採用字串。

現在,顯示使用者輸入的字串 -

示例

 線上演示

using System;
using System.Collections.Generic;

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

      // use ReadLine() to read the entered line
      myStr = Console.ReadLine();

      // display the line
      Console.WriteLine("Result = {0}", myStr);
   }
}

輸出

Result =

以下是輸出。假設使用者輸入了“Amit”作為輸入 -

Result = amit

更新於: 22-Jun-2020

7K+ 瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.