EN
C# / .NET - remove first 3 characters from string
0
points
In this article, we would like to show you how to remove the first 3 characters from the string in C# / .NET.
Quick solution:
string text = "ABCDE";
string result = text.Substring(3);
Console.WriteLine(result); // DE
Practical examples
1. Using String Substring() method
In this example, we use String Substring() method to create a new result substring from the text string without the first 3 characters.
Syntax:
Substring(int startIndex, int length);
Note:
If
lengthis not given, substring will be done fromstartIndexto the end of the text.
Practical example:
using System;
public class StringUtils
{
public static void Main(string[] args)
{
string text = "ABCDE";
string result = text.Substring(3);
Console.WriteLine(result); // DE
}
}
Output:
DE
2. Using StringBuilder Remove() method
In this example, we create sb StringBuilder object from the text string, then we use Remove() method on the sb to delete the first 3 characters.
Syntax:
Remove(int startIndex, int length)
Practical example:
using System;
using System.Text;
public class StringUtils
{
public static void Main(string[] args)
{
string text = "ABCDE";
// Create StringBuilder object
StringBuilder sb = new(text);
// Remove first 3 characters
sb.Remove(0, 3);
Console.WriteLine(sb);
}
}
Output:
DE