Languages
[Edit]
EN

C# / .NET - remove substring from string between indexes

0 points
Created by:
Dirask Admin - Marvin
1850

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

C# / .NET - String (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join