How do I replace a text in a string?

Category: System, viewed: 2K time(s).

An example for showing how to replace a portion of text in the string.

using System;
using System.Text;

namespace Kodecsharp.Example.System
{
    public class ReplaceText
    {
        public static void Main(string[] args)
        {
            //
            // Replacing a portion of string with another text.
            //
            string text = "A, B, C, D, E, F, G, H, I, J";
            text = text.Replace(",", "");
            Console.WriteLine(text);

            //
            // Replacing a specified text of a string with another text.
            //
            StringBuilder builder = new StringBuilder("Email: @EMAIL@");
            builder.Replace("@EMAIL@", "someone@somewhere.com");
            Console.WriteLine(builder.ToString());

            Console.ReadLine();
        }
    }
}
Powered by Disqus