Get the First Character of a String in C#
-
Get the First Character of a String With the Linq Method in
C#
-
Get the First Character of a String With the
String[]
Method inC#
-
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.
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.
LinkedInRelated Article - Csharp String
- C# Convert String to Enum
- C# Convert Int to String
- Use Strings in Switch Statement in C#
- Convert a String to Boolean in C#
- Convert a String to Float in C#
- Convert a String to a Byte Array in C#
Related Article - Csharp Char
- C# Convert a Char to an Int
- Convert Char to Int in C#
- Count Occurrences of a Character Inside a String in C#
- Get ASCII Value of Character in C#
- Remove a Character From a String in C#