How do I insert a text into String?
Category: System, viewed: 4K time(s).
In this example we use the String.Insert() and StringBuilder.Insert() methods to insert a text into a string.
using System;
using System.Text;
namespace Kodecsharp.Example.System
{
public class InsertText
{
public static void Main(string[] args)
{
string text = "The fox jumps over the lazy dog";
//
// Insert a text into string using String.Insert() method. The method
// accept the index where the new text will be inserted.
//
text = text.Insert(4, "quick brown ");
Console.WriteLine(text);
//
// Insert a text into string using StringBuilder.Insert() method.
//
StringBuilder builder = new StringBuilder("Jackdaws love my big quartz");
builder.Insert(21, "sphinx of ");
Console.WriteLine(builder.ToString());
Console.ReadLine();
}
}
}