如何在 C# 中查找字符串的第一个字符?

csharpprogrammingserver side programming

要获取第一个字符,请使用 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


相关文章