Languages
[Edit]
EN

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

0 points
Created by:
Wade
562

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# / .NET - String (popular problems)

C# / .NET - remove substring from string between indexes
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