Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find the first character of a string in C#?
To get the first character of a string in C#, you can use several approaches. The most common methods are using string indexing, the Substring() method, or the First() LINQ method.
Syntax
Following are the different syntaxes to get the first character −
char firstChar = str[0];
string firstChar = str.Substring(0, 1);
char firstChar = str.First();
Using String Indexing
The simplest way to get the first character is using string indexing with [0]. This returns a char type −
using System;
public class Demo {
public static void Main() {
string str = "Welcome to the Planet!";
char firstChar = str[0];
Console.WriteLine("First character: " + firstChar);
Console.WriteLine("Character type: " + firstChar.GetType());
}
}
The output of the above code is −
First character: W Character type: System.Char
Using Substring() Method
The Substring() method extracts a portion of the string. To get the first character, use Substring(0, 1) which returns a string type −
using System;
public class Demo {
public static void Main() {
string str = "Welcome to the Planet!";
string firstChar = str.Substring(0, 1);
Console.WriteLine("First character: " + firstChar);
Console.WriteLine("Return type: " + firstChar.GetType());
}
}
The output of the above code is −
First character: W Return type: System.String
Using LINQ First() Method
The LINQ First() method returns the first element of a sequence. For strings, it returns the first character as a char −
using System;
using System.Linq;
public class Demo {
public static void Main() {
string str = "Welcome to the Planet!";
char firstChar = str.First();
Console.WriteLine("First character: " + firstChar);
// First() with condition
char firstVowel = str.First(c => "aeiouAEIOU".Contains(c));
Console.WriteLine("First vowel: " + firstVowel);
}
}
The output of the above code is −
First character: W First vowel: e
Handling Empty Strings
When working with potentially empty strings, you need to check the string length to avoid exceptions −
using System;
using System.Linq;
public class Demo {
public static void Main() {
string emptyStr = "";
string normalStr = "Hello";
// Safe indexing
char firstChar1 = normalStr.Length > 0 ? normalStr[0] : '\0';
char firstChar2 = emptyStr.Length > 0 ? emptyStr[0] : '\0';
Console.WriteLine("Normal string first char: '" + firstChar1 + "'");
Console.WriteLine("Empty string first char: '" + firstChar2 + "'");
// Using FirstOrDefault() - safer with LINQ
char safeFirst = normalStr.FirstOrDefault();
char safeEmpty = emptyStr.FirstOrDefault();
Console.WriteLine("Safe normal: '" + safeFirst + "'");
Console.WriteLine("Safe empty: '" + safeEmpty + "'");
}
}
The output of the above code is −
Normal string first char: 'H' Empty string first char: '' Safe normal: 'H' Safe empty: ''
Comparison of Methods
| Method | Return Type | Performance | Empty String Handling |
|---|---|---|---|
| str[0] | char | Fastest | Throws IndexOutOfRangeException |
| str.Substring(0, 1) | string | Slower | Throws ArgumentOutOfRangeException |
| str.First() | char | Fast | Throws InvalidOperationException |
| str.FirstOrDefault() | char | Fast | Returns '\0' for empty strings |
Conclusion
The fastest way to get the first character is using string indexing str[0], which returns a char. For safer operations with potentially empty strings, use FirstOrDefault() or check string length before accessing. Use Substring(0, 1) only when you specifically need a string return type.
