Languages
[Edit]
EN

C# / .NET - string replace

1 points
Created by:
AnnLen
1795

In C# / .NET it is possible to replace part of string in few ways.

1. String.Replace method example

string input = "My name is John.";
string output = input.Replace("John", "Kate");

Console.WriteLine(output);

Output:

My name is Kate.

2. Regex.Replace instance method example

Regex regex = new Regex("John");

string input = "My name is John.";
string output = regex.Replace(input, "Kate");

Console.WriteLine(output);

Output:

My name is Kate.

3. Regex.Replace static method example

string pattern = "John";

string input = "My name is John.";
string output = Regex.Replace(input, pattern, "Kate");

Console.WriteLine(output);

Output:

My name is Kate.

4. References

  1. String.Replace Method - Microsoft Docs
  2. Regex.Replace Instance Method - Microsoft Docs
  3. Regex.Replace Static Method - Microsoft Docs
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.