EN
C# / .NET - replace all occurrences of string
0 points
In this article, we would like to show you how to replace all occurrences of the string in C# / .NET.
Quick solution:
xxxxxxxxxx
1
string text = "ABC ABC ABC";
2
string replacement = "X";
3
string result = text.Replace("C", replacement);
4
5
Console.WriteLine(result); // ABX ABX ABX
In this example, we use Replace()
method to replace all occurrences of the "C
" in the text
string with "X
".
xxxxxxxxxx
1
using System;
2
3
public class StringUtils
4
{
5
public static void Main(string[] args)
6
{
7
string text = "ABC ABC ABC";
8
string replacement = "X";
9
string result = text.Replace("C", replacement);
10
11
Console.WriteLine("Original text: " + text); // ABC ABC ABC
12
Console.WriteLine("Modified text: " + result); // ABX ABX ABX
13
}
14
}
Output:
xxxxxxxxxx
1
Original text: ABC ABC ABC
2
Modified text: ABX ABX ABX