如何在 C# 中查詢字串的第一個字元?


若要獲取第一個字元,請使用 substring() 方法

假設我們的字串如下 −

string str = "Welcome to the Planet!";

現在,若要獲取第一個字元,請在 substring() 方法中設定值 1。

string res = str.Substring(0, 1);

讓我們看看完整的程式碼 −

範例

 線上演示

using System;
public class Demo {
   public static void Main() {
      string str = "Welcome to the Planet!";
      string res = str.Substring(0, 1);
      Console.WriteLine(res);
   }
}

輸出

W

更新於: 10 - 9 - 2023

40K+ 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始吧
廣告
© . All rights reserved.