EN
C# / .NET - remove substring from string between indexes
0 points
In this article, we would like to show you how to create our own function to remove substring from a string in C#.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static string RemoveSubstring(String text, int startIndex, int endIndex)
6
{
7
if (endIndex < startIndex)
8
{
9
startIndex = endIndex;
10
}
11
12
string a = text.Substring( 0, startIndex );
13
string b = text.Substring( endIndex );
14
15
return a + b;
16
}
17
18
public static void Main(string[] args)
19
{
20
// Usage example:
21
22
// index: 0 5 7 11 14
23
// | | | | |
24
string text = "This is my text";
25
26
Console.WriteLine( RemoveSubstring(text, 0, 5)); // is my text
27
Console.WriteLine( RemoveSubstring(text, 5, 7)); // This my text
28
Console.WriteLine( RemoveSubstring(text, 1, text.Length - 1)); // Tt
29
}
30
}
Output:
xxxxxxxxxx
1
is my text
2
This my text
3
Tt