Chapter15:StringInterpolation

No Comments

Section15.1:Formatdatesinstrings

vardate=newDateTime(2015,11,11);

varstr=$”It’s{date:MMMMd,yyyy},makeawish!”;

System.Console.WriteLine(str);

YoucanalsousetheDateTime.ToStringmethodtoformattheDateTimeobject.Thiswillproducethesameoutput as the code above.

vardate=newDateTime(2015,11,11);

varstr=date.ToString(“MMMMd,yyyy”);

str=”It’s”+str+”,makeawish!”;

Console.WriteLine(str);

Output:

It’sNovember11,2015,makeawish!

LiveDemoon.NETFiddle

LiveDemousingDateTime.ToString

Note:MMstandsformonthsandmmforminutes.Beverycarefulwhenusingtheseasmistakescan introduce bugs that may be difficult to discover.

Section15.2:Paddingtheoutput

String can be formatted to accept a padding parameter that will specify how many character positions the inserted string will use :

${value,padding}

NOTE:Positivepaddingvaluesindicateleftpaddingandnegativepaddingvaluesindicaterightpadding.

LeftPadding

A left padding of 5 (adds 3 spaces before the value of number, so it takes up a total of 5 character positions in the resulting string.)

varnumber=42;

varstr=$”Theanswertolife,theuniverseandeverythingis{number,5}.”;

//stris”Theanswertolife,theuniverseandeverythingis                                           42.”;

//                                                                                                                                            ^^^^^

System.Console.WriteLine(str);

Output:

Theanswertolife,theuniverseandeverythingis 42.

LiveDemoon.NETFiddle

RightPadding

Rightpadding,whichusesanegativepaddingvalue,willaddspacestotheendofthecurrentvalue.

varnumber=42;

varstr=$”Theanswerto life,theuniverseandeverything is${number,-5}.”;

//stris”Theanswertolife,theuniverseandeverythingis42                                          .”;

//                                                                                                                                            ^^^^^

System.Console.WriteLine(str);

Output:

Theanswertolife,theuniverseandeverythingis42                                 .

LiveDemoon.NETFiddle

PaddingwithFormatSpecifiers

Youcanalsouseexistingformattingspecifiersinconjunctionwithpadding.

varnumber=42;

varstr=$”Theanswertolife,theuniverseandeverythingis${number,5:f1}”;

//stris”Theanswertolife,theuniverseandeverythingis42.1“;

//             ^^^^^

LiveDemoon.NETFiddle

Section15.3:Expressions

Fullexpressionscanalsobeusedininterpolatedstrings.

varStrWithMathExpression=$”1+2={1+2}”;//->”1+2=3″

stringworld=”world”;

varStrWithFunctionCall=$”Hello,{world.ToUpper()}!”;//->”Hello,WORLD!”

LiveDemoon.NETFiddle

Section15.4:Formattingnumbersinstrings

Youcanuseacolonandthestandardnumericformatsyntaxtocontrolhownumbersareformatted.

vardecimalValue=120.5;

varasCurrency=$”Itcosts{decimalValue:C}”;

//Stringvalueis”Itcosts$120.50″(dependingonyourlocalcurrencysettings)

varwithThreeDecimalPlaces=$”Exactly{decimalValue:F3}”;

//Stringvalueis”Exactly120.500″

varintegerValue=57;

varprefixedIfNecessary=$”{integerValue:D5}”;

//Stringvalueis”00057″

LiveDemoon.NETFiddle

Section15.5:SimpleUsage

varname=”World”;

varstr=$”Hello,{name}!”;

//strnowcontains:”Hello,World!”;

Behindthescenes

Internallythis

$”Hello,{name}!”

Willbecompiledtosomethinglikethis:

string.Format(“Hello,{0}!”,name);

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts
No Comments