Insert variable values in the middle of a string in C#
To insert variable values in the middle of a string in C# we can usestring.Format
method that will take the template string as first parameter followed by indexed parameters we need to replaceExample
using System;
public class Example
{
public static void Main()
{
string template = "Hello, the following fruits: {0}, {1} and {2} are available today";
string orange = "Orange";
string apple = "Apple";
string banana = "Banana";
string message = string.Format(template, orange, apple, banana);
Console.WriteLine(message);
}
}
Output
Hello, the following fruits: Orange, Apple and Banana are available today