Get the First Character of a String in C#

  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

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.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp String

  • Convert a String to a Byte Array in C#
  • Truncate a String in C#
  • Related Article - Csharp Char

  • Validate Email Address in C#
  • Convert Enum to String in C#