如何在 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

更新於:2023 年 9 月 10 日

超過 40K 的瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.