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:
xxxxxxxxxx
1
string text = "ABCDE";
2
3
string result = text.Substring(3);
4
Console.WriteLine(result); // DE
In this example, we use String Substring()
method to create a new result
substring from the text
string without the first 3 characters.
Syntax:
xxxxxxxxxx
1
Substring(int startIndex, int length);
Note:
If
length
is not given, substring will be done fromstartIndex
to the end of the text.
Practical example:
xxxxxxxxxx
1
using System;
2
3
public class StringUtils
4
{
5
public static void Main(string[] args)
6
{
7
string text = "ABCDE";
8
9
string result = text.Substring(3);
10
Console.WriteLine(result); // DE
11
}
12
}
Output:
xxxxxxxxxx
1
DE
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:
xxxxxxxxxx
1
Remove(int startIndex, int length)
Practical example:
xxxxxxxxxx
1
using System;
2
using System.Text;
3
4
public class StringUtils
5
{
6
public static void Main(string[] args)
7
{
8
string text = "ABCDE";
9
10
// Create StringBuilder object
11
StringBuilder sb = new(text);
12
13
// Remove first 3 characters
14
sb.Remove(0, 3);
15
16
Console.WriteLine(sb);
17
}
18
}
Output:
xxxxxxxxxx
1
DE