Techiehook Techiehook
Updated date Aug 10, 2024
In this post, we will learn how to manipulate strings in C# with methods for adding, removing, and replacing substrings.

How to Add, Remove, and Replace Substrings in C#?

In C#, we can add, delete/remove, or replace substrings within a string using various methods. We are going to discuss some examples below,

1. Add a String

To add (or concatenate) a string, we can use the + operator, String.Concat, or StringBuilder.

string text1 = "Hello";
string text2 = " TechieHook";

// Using the + operator
string result = text1 + text2; // "Hello TechieHook"

// Using String.Concat
string result2 = String.Concat(text1, text2); // "Hello TechieHook"

// Using StringBuilder (efficient for multiple additions)
StringBuilder sb = new StringBuilder(text1);
sb.Append(text2);
string result3 = sb.ToString(); // "Hello TechieHook"

2. Delete/Remove a Substring

We can remove a substring using the Remove method or by using Substring.

string text3 = "Hello TechieHook";

// Removing a substring starting at index 5 and removing 6 characters
string result = text3.Remove(5, 11); // "Hello"

// Using Substring to remove from the start to a specific length
string result2 = text3.Substring(0, 5); // "Hello"

3. Replace a Substring

The Replace method is used to replace a substring in C#, we can see the sample code snippet below,

string text4 = "Hello TechieHook";

// Replacing "Hello" with "Welcome"
string result = text4.Replace("Hello", "Welcome"); // "Welcome TechieHook"

4. Advanced Replacement using Regular Expressions

We can also use Regex.Replace for more complex pattern-based replacements shown below,

using System.Text.RegularExpressions;

string text5 = "Hello 2024 TechieHook";

// Replace digits with a hashtag
string result = Regex.Replace(text5, @"\d", "#"); // "Hello #### TechieHook"

Full program:

using System;
using System.Text;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // Example 1: Add a String
        string text1 = "Hello";
        string text2 = " TechieHook";

        // Using the + operator
        string result = text1 + text2;
        Console.WriteLine("After Addition: " + result); 

        // Example 2: Remove a Substring
        string text3 = "Hello TechieHook";

        // Removing a substring starting at index 5 and removing 6 characters
        string removedResult = text3.Remove(5, 11);
        Console.WriteLine("After Removal: " + removedResult);

        // Example 3: Replace a Substring
        string text4 = "Hello TechieHook";

        // Replacing "Hello" with "Welcome"
        string replacedResult = text4.Replace("Hello", "Welcome");
        Console.WriteLine("After Replacement: " + replacedResult);

        // Example 4: Advanced Replacement using Regular Expressions
        string text5 = "Hello 2024 TechieHook";

        // Replace digits with a hashtag
        string regexReplacedResult = Regex.Replace(text5, @"\d", "#");
        Console.WriteLine("After Regex Replacement: " + regexReplacedResult);
        Console.ReadKey();
    }
}

Output:

After Addition: Hello TechieHook
After Removal: Hello
After Replacement: Welcome TechieHook
After Regex Replacement: Hello #### TechieHook

ABOUT THE AUTHOR

Techiehook
Techiehook
Admin, Australia

Welcome to TechieHook.com! We are all about tech, lifestyle, and more. As the site admin, I share articles on programming, tech trends, daily life, and reviews... For more detailed information, please check out the user profile

https://www.techiehook.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!