Get the First Character of a String in C#

Muhammad Maisam Abbas Mar 19, 2021
  1. Get the First Character of a String With the Linq Method in C#
  2. Get the First Character of a String With the String[] Method in C#
  3. Get the First Character of a String With a User-Defined Method in C#
Get the First Character of a String in C#

This tutorial will discuss the methods to get the first character of a string variable in C#.

Get the First Character of a String With the Linq Method in C#

Linq is used to integrate SQL queries on data structures in C#. The FirstOrDefault() function gets the first character of a string. The following code example shows us how we can get the first character of a string with the Linq method in C#.

using System;
using System.Linq;

namespace get_first_char_of_string
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "DelftStack";
            char first = name.FirstOrDefault();
            Console.WriteLine(first);
        }
    }
}

Output:

D

In the above code, we extracted the first character first from the string variable name with the FirstOrDefault() function in C#.

Get the First Character of a String With the String[] Method in C#

The String[x] property gets the character on the x index of a string in C#. The following code example shows us how we can get the first character of a string variable with the String[] property in C#.

using System;
using System.Linq;

namespace get_first_char_of_string
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "DelftStack";
            char first = name[0];
            Console.WriteLine(first);
        }
    }
}

Output:

D

In the above code, we extracted the first character first from the string variable name with name[0] in C#.

Get the First Character of a String With a User-Defined Method in C#

We can also get the first character of a string by following a user-defined approach in C#. For this, we have to first convert the string to an array of characters with the String.ToCharArray() function in C#. After that, we can get the character at the 0 index of the character array. The following code example shows us how we can implement this logic in C#.

using System;
using System.Linq;

namespace get_first_char_of_string
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "DelftStack";
            char[] charArray = name.ToCharArray();
            char first = charArray[0];
            Console.WriteLine(first);
        }
    }
}

Output:

D

This code does the same thing as the other two examples discussed above. But this approach is not recommended because it takes more time and resources to execute, and a built-in method already exists for this.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Csharp String

Related Article - Csharp Char