How to Search & Replace Strings in C#
In C# replace is an easy
method to find an old value in a string and replace it (and all instances of
it) with a new value of your choice. This method, will apply named the Replace
method, is fairly straightforward to understand.
public string Replace
(
string oldValue,
string newValue
}
The finest way to know how to use a method, though, is to
see it in action. So let’s say that we have the following string:
String a = "Hello";
a = a.Replace("Hello ", "Good morning");
Console.WriteLine(a);.
Example;
using System;
public class Example
{
public static void Main()
{
String s = new String('a', 3);
Console.WriteLine("The initial
string: '{0}'", s);
s = s.Replace('a', 'b').Replace('b', 'c').Replace('c', 'd');
Console.WriteLine("The final
string: '{0}'", s);
Console.Read();
}
}
0 Comments