Daily C# - (#2) – Composite Format Strings in C#

in #charp6 years ago

When working with C#, you’ll often want to create a string that is composed of some combination of static text and substituted variable values. You do this by specifying a format string that contains a list of placeholders (format items) where the variable values will be substituted. You also specify the actual values of the variables to be substituted.

Below is an example. Here we substitute both string and integer variables. The format items within the format string are indicated with a zero-based index surrounded by curly braces. The index indicates which object in the list should be substituted.

string name = "Sean";
int age = 46;
string sFinal = string.Format("{0} is {1} yrs old. {0} is old.", name, age);

This results in the string: Sean is 46 yrs old. Sean is old.

Till next time :)

Coin Marketplace

STEEM 0.27
TRX 0.13
JST 0.032
BTC 63159.84
ETH 2972.58
USDT 1.00
SBD 3.57