Languages
[Edit]
EN

C# / .NET - replace all occurrences of string

0 points
Created by:
Theodora-Battle
528

In this article, we would like to show you how to replace all occurrences of the string in C# / .NET.

Quick solution:

string text = "ABC ABC ABC";
string replacement = "X";
string result = text.Replace("C", replacement);

Console.WriteLine(result);  // ABX ABX ABX

 

1. Practical example using string Replace() method

In this example, we use Replace() method to replace all occurrences of the "C" in the text string with "X".

using System;

public class StringUtils
{
    public static void Main(string[] args)
    {
        string text = "ABC ABC ABC";
        string replacement = "X";
        string result = text.Replace("C", replacement);

        Console.WriteLine("Original text: " + text);    // ABC ABC ABC
        Console.WriteLine("Modified text: " + result);  // ABX ABX ABX
    }
}

Output:

Original text: ABC ABC ABC
Modified text: ABX ABX ABX

References

  1. String.Replace Method - .NET Docs
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.

Cross technology - replace all occurrences of string

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