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#.
Practical example
using System;
public class Program
{
static string RemoveSubstring(String text, int startIndex, int endIndex)
{
if (endIndex < startIndex)
{
startIndex = endIndex;
}
string a = text.Substring( 0, startIndex );
string b = text.Substring( endIndex );
return a + b;
}
public static void Main(string[] args)
{
// Usage example:
// index: 0 5 7 11 14
// | | | | |
string text = "This is my text";
Console.WriteLine( RemoveSubstring(text, 0, 5)); // is my text
Console.WriteLine( RemoveSubstring(text, 5, 7)); // This my text
Console.WriteLine( RemoveSubstring(text, 1, text.Length - 1)); // Tt
}
}
Output:
is my text
This my text
Tt