How to find the first character of a string in C#?

CsharpProgrammingServer Side Programming

To get the first character, use the substring() method.

Let’s say the following isour string −

string str = "Welcome to the Planet!";

Now to get the first character, set the value 1 in the substring() method.

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

Let us see the complete code −

Example

 Live Demo

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

Output

W
raja
Published on 21-Aug-2018 09:06:09
Advertisements