Replace a String in C#

There are several different situations where you will want or need to know how to replace a string in C#. For example, if you need to change the word “blue” with “red” somewhere in a document, you might use a replacement operation to do so. Replacing strings in C# is straightforward once you understand the syntax and sequence of operations.

Accordingly, in C#, you can replace a string with another string just like in other programming languages such as Java and C++. In this tutorial, we’ll be discussing how you can replace a string in C# with different methods.

  • Using the string.Replace() Method
  • Using the StringBuilder.Replace() Method
  • Using the Regex.Replace() Method

Each of these approaches comes with its advantages and disadvantages. Let’s take a look at them now with the help of examples.

 

Using the String.Replace() Method

The most straightforward way to replace a string in C# is to use the built-in method String.Replace()..

Syntax

string.replace (Oldvalue, Newvalue)

Parameters

With this method, you pass in the following parameters:

  • Oldvalue: The string that you want to replace. This can be a literal string or a variable holding a string.
  • Newvalue: The string that you want to replace it with. This can be a literal string or a variable holding a string.

Return Value

This method returns the String object after replacing the Oldvalue with Newvalue.

Example

The following example shows the String.Replace() method in C#.

using System;
    using System.Text.RegularExpressions;

    public class Example
    {
       public static void Main()
       {
          string str = "New C sharp Tutorial";
    
       // replaces "New" with "Best" 
       
      string result = str.Replace("New", "Best");
      Console.WriteLine(result);

      Console.ReadLine();
       }
    }

When the above code is compiled and executed, it produces the following result:

Output

Best C sharp Tutorial

 

Using the StringBuilder.Replace() Method

The StringBuilder.Replace() method in C# is used to replace the specified old value with the new value in the current StringBuilder object. This method returns a new StringBuilder object that represents the modified string.

Syntax

StringBuilder.Replace(remValue, repValue)

 

Parameters

  • remValue: It is the string to be replaced.
  • repValue: It is the string that will replace the remValue.

Return Value

This method returns the StringBuilder object after replacing the remValue with repValue.

Example

The following example shows the StringBuilder.Replace() method in C#.

using System;
 using System.Text;
 class Example
 {
     public static void Main()
     {
      StringBuilder str = new StringBuilder("Hello this is First C sharp Tutorial");
 
         // Replace 'First' with 'Best'
      str.Replace("First", "Best");
 
         // Display the string.
      Console.WriteLine(str);
     }
 }

When the above code is compiled and executed, it produces the following result:

Hello this is Best C sharp Tutorial 

Using the Regex.Replace() Methods

The Regex.Replace() method in C# is used to replace the characters in a specified input string that matches a specified regular expression pattern with a specified replacement string. The input string can be any character sequence, and the replacement string can contain any characters. Optionally, Regex.Replace() can take a regular expression pattern as its first argument.

Syntax                

Regex.Replace(input, replacementPattern, replacementText);

Parameters

  • input: It is the string to be replaced,
  • replacementPattern: It is the regular expression pattern to match
  • replacementText: Is the replacement text.

The fifth and sixth parameters are also optional and specify the regular expression options and the time-out interval for the match respectively. 

Example
The following example illustrates the use of the Regex.Replace() method to replace all occurrences of the word “New” with the word “Best”.

using System;
    using System.Text.RegularExpressions;

    public class Example
    {
       public static void Main()
       {
          string input = "New C sharp Tutorial";
          string pattern = "New";
          string replacement = "Best";
          Console.WriteLine(Regex.Replace(input, pattern, replacement));
       }
    }

When the above code is compiled and executed, it produces the following result:

Output

Best C sharp Tutorial

Summery

The most straightforward way to replace a string in C# is to use the built-in method String.Replace(). With this method, you pass in the following parameters: – The string that you want to replace – This can be a literal string or a variable holding a string. – The string that you want to replace it with – This can be a literal string or a variable holding a string. The target string that you want to search for – This can be a literal string or a variable holding a string. Replacement options – There are two different options you can use for this parameter: Moreover, you can also use string replacement functions or regular expressions to replace a string in C#. Depending on your needs, you may want to use one over the others.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.