In this C#.Net tutorial, we will discuss, how to get first n characters of string in c#.net. We will see a complete example using visual studio 2022.
Substring Method to Extract the First N Characters in a String in C#
In C#.Net, the Substring() method of the string class is commonly used to extract a portion of the string – in this case, the first n characters.
Here’s a simple example of how to use it:
string originalString = "Hello, World!";
int n = 5;
if (originalString.Length >= n)
{
string firstNCharacters = originalString.Substring(0, n);
Console.WriteLine(firstNCharacters); // Outputs: "Hello"
}
else
{
Console.WriteLine($"The string length is less than {n}");
}
In the code above:
- We first declare a string
originalStringthat contains “Hello, World!” and an integernthat is set to 5. - We then use an
ifstatement to check whether the length of the string is greater than or equal ton. This is important to prevent anyArgumentOutOfRangeExceptionthat would occur ifnwere greater than the length of the string. - Inside the
ifblock, we use theSubstring() method. This method takes two arguments: the start index and the length of the substring. Since we want the firstncharacters, we start from index 0 (which is the beginning of the string) and specifynas the length of the substring. - The resulting substring (
firstNCharacters) is then printed to the console. - If the length of the original string is less than
n, a message is printed to indicate this.
The Substring method is very handy when it comes to manipulating strings in C#. Remember to always check the length of the original string before using it to avoid out-of-range exceptions.
Get first n characters of string c#.net – Complete example
Now, let us see a complete example of how to get the first n characters of a string in C#.Net.
Step 1: Setting Up Your Development Environment
Before writing the code, you need to have a development environment. If you don’t have it set up, follow these steps:
- Download Visual Studio: You can download the Visual Studio IDE from the official Microsoft website. Choose the community version, which is free.
- Install Visual Studio: Follow the installer instructions to install Visual Studio 2022 on your system. The default settings should work for most users.
- Create a New Project: Open Visual Studio and create a new project. You can select the “Console App (.NET Core)” template.
- Name Your Project: Name your project and select a location to save it.
Step 2: Writing the Code
Here is a simple example of how to get the first n characters from a string in C#.net:
using System;
class Program
{
static void Main()
{
string str = "Hello, World!";
int n = 5;
if (str.Length >= n)
{
string substr = str.Substring(0, n);
Console.WriteLine(substr);
}
else
{
Console.WriteLine("The length of the original string is less than " + n);
}
}
}
In this code:
- We first declare a string
strthat contains “Hello, World!” and an integernset to 5. - We then use an
ifstatement to check whether the length of the string is greater than or equal ton. This is to avoid anyArgumentOutOfRangeExceptionthat would occur ifnwere greater than the length of the string. - The
Substringmethod of thestringclass is used to get the firstncharacters from the string. The method takes two arguments: the start index (in this case 0, as we want to start from the beginning of the string) and the length of the substring. - The resulting substring is then printed to the console using
Console.WriteLine. - If the length of the original string is less than
n, a message is printed to indicate this.
Step 3: Running the Code
After you’ve written the code:
- Press
F5or click on theStart Debuggingbutton in Visual Studio 2022 to run your code. - The output will be displayed in the console window.

Conclusion
Through this tutorial, we understand how to get the first n characters from a string in C#.Net.
You may also like the following C#.Net tutorials:
- Convert Int to Binary String with Leading Zeros in C#.Net
- Assign Null Value to a String in C#.NET
- Difference Between String and StringBuilder in C#.NET With Example
- remove a particular character from string C#.net
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…