aAbrEABDBEBtErBeX-~~C| 64956VC 95V~BWB~C 65976VB 7566V~C 688646VPWCC 4VC~~C 594598}55Z 64VB-~CBZZCZZZ-

Format.` Among the distant rocks you see a sparkle of light. You discover a mysterious tablet with an ancient message upon it. But it is formatted strangely. You cannot read it.`With formatting,` we change how data appears. The string.Format method helps. We use it to change how numbers are printed. We use format codes.`First,` we use string.Format to combine 3 strings with formatting options. The format string itself is the first argument. It is usually specified as a string literal. `Markers: `The 0, 1 and 2 are where the first, second and third arguments are inserted. A format specification comes after the ":" char.`Variables: `The program formats 3 variables. These are a string, an int and a DateTime struct.`Result: `The string has formatting applied. The int is displayed with a decimal place. The year is displayed with 4 digits.`Number formats.` We can specify that a value (like a double) can be formatted inside string.Format. A format string is the first argument to string.Format. `The format string in this example uses the 0:0.0% syntax. This means that the second argument is formatted with the pattern 0.0%.`Numbers, percentages.` The 0.0% part of the format string specifies the number of digits. The format code can have many digits before the decimal place, but only one after it. `Quote: `The presence of a "%" character in a format string causes a number to be multiplied by 100 before it is formatted.`Custom Numeric Format Strings: MSDN `https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx`Padding.` This can be expressed declaratively in format strings. Padding inserts characters at the left or right of the string to reach a specified length. `Instead of the PadLeft and PadRight methods, you can use the string.Format method with special substitutions.`PadRight, PadLeft `padright`Sizes: `Use the comma char followed by the padding size. A negative number will add padding to the right (left-align).`You can use a positive padding size to add padding to the left. This will right-align the string.`ToString.` Sometimes, you need to just format a single number, like an integer or long. In this case, you don't need to use string.Format. You can just use the ToString virtual method. `ToString `tostring`Hex` is a number representation. We use format strings and the ToString method to convert ints to hex format. The int.Parse method can then be used to convert them back. `Code: `X formats as hexadecimal. You can specify X and then a number (such as X8) to pad the output on the left side with zeros.`You can use the NumberStyles.AllowHexSpecifier argument to make int.Parse handle hex numbers.`FormatException.` How can you solve this problem? System.FormatException is thrown by methods that receive format strings and substitutions. `FormatException continued.` Format strings use substitution markers. The arguments after the format string are placed in those markers in the same order. `The System.FormatException is thrown because the {2} substitution marker was not found in the argument list.`To fix the program, you could remove the substitution marker {2} or add two more arguments.`Tip 2: `Whenever you encounter a FormatException, it is worthwhile to check substitution markers and argument lists for formatting methods.`Performance.` When creating complex strings, we are tempted to use concatenations. But we can achieve better performance by unifying the string operations. `We look at a program that compares 2 methods of creating a formatted string.`Method 1: `The Method1 version of the logic uses a single format string. The MB part is inside the format string.`Method 2: `This version uses a format string and then another concatenation after that.`Result: `Method1 is faster. With string.Format and ToString, we can combine literal concatenations in the format for better performance.`String interpolation.` For simple format strings, we can use string interpolation instead of the string.Format method. This has clearer syntax that is validated by the compiler. `String Interpolation `string-interpolation`Dates.` DateTime, a struct, represents any possible date and time. The string.Format method can be used with DateTime arguments. These will be inserted into the substitution markers. `DateTime, Format `datetime-format`File names.` Many programs need to create files dynamically. The files often need to have file names that are based on some characteristic. `For example: `A logging file can have a file name that is based on the exact date and time it was written.`You can use the string.Format method with substitutions to create the file names based on state variables.`Filename, DateTime `filename-datetime`StringBuilder.` This class has a method called AppendFormat. The AppendFormat method receives a formatting string and the arguments to the formatting string. `AppendFormat `appendformat`Console.` Programs can use format strings directly inside the Console.Write and Console.WriteLine methods. In Visual Studio, type Console.WriteLine. Scroll through the IntelliSense window. `Console.WriteLine `console`Then: `You can call Console.WriteLine with the same arguments as the string.Format method receives. It will call string.Format.`Internals.` The string.Format method is implemented with a params argument—this is a variable parameter list. This results in an array allocation on each invocation of the method. `Params `params`Internals, substitution processing.` String.Format uses StringBuilder, which contains a mutable character buffer. It estimates a capacity based on a heuristic. `StringBuilder Capacity `stringbuilder-capacity`Internals, AppendFormat.` We find the AppendFormat method is called to process the substitutions themselves. The ToString method is called. It does not normally require a copy to be made. `StringBuilder ToString `stringbuilder-tostring`A summary.` With Format, we insert argument strings and separators together. We can specify display options for each argument. We combined strings and dates and numbers.

OVKQjKKVDVQVbVJVAK{KOV%O{jKOOV{Declare three variables.KOOV9The Vhs they have are not important.KOOjVP Vh1VyjQV(Qj;KOOViVh2Vy10000;KOOV. Vh3VyVqV.(2015, 11, 1);jKOOV{Use VP.Format mVg with four arguments.KOOV9The first argument is the V|matting VP.KOOV9It specifies how the next arguments are V|matted.KOOjVP VMVyjVP.Formatj(jQ{0}: {1:0.0} - {2:yyyy}Qj,KOOOVh1,KOOOVh2,KOOOVh3);jKOOV{Va the VM.KOOjV'VM);KO}K}KKjKKV(: 10000.0 - 2015jKKVDVQVbVJVAK{KOV%O{jKOOV{Format a ratio as a percentage VP.KOOV9You must specify the percentage symbol.KOOV9It will multiply the Vh by 100.KOOjdouble ratioVy0.73;KOOVP VMVyjVP.Formatj(jQVPVy{0:0.0%}Qj,KOOOratio);KOOV'VM);KO}K}KKjKKVPVy73.0%jKKVDVQVbVJVAK{KOV%O{jKOOV{The constant V|matting VP.KOOV9It specifies the padding.KOOV9A negative VE meansVjleft-align.KOOV9A positive VE meansVjright-align.KOOjconst VP V|matVyjQ{0,-10} {1,10}Qj;jKOOV{Construct the VPs.KOOjVP line1VyjVP.Formatj(V|mat,KOOO100,KOOO5);KOOVP line2VyjVP.Formatj(V|mat,KOOOQCarrotQ,KOOOQGiraffeQ);jKOOV{Va the V|matted VPs.KOOjV'line1);KOOV'line2);KO}K}KKjKK100OOOO 5KCarrotOOGiraffejKKVDVQVbVJVAK{KOV%O{KOOViVhVy123;KOOVP aVyjVP.Formatj(jQ{0:0000}Qj, Vh);j V{Too complex.KOOjVP bVyVh.ToVO(Q0000Q);j V{Simpler.KOOjV'a);KOOV'b);KO}K}KKjKK0123K0123jKKVDVQ;KVDVQ.GlobalizationVbVJVAK{KOV%O{KOOViVh1Vy10995;jKKOOV{Va VEVphex V|mat.KOOjV'jQ{0:x}Qj, Vh1);KOOV'jQ{0:x8}Qj, Vh1)VbOOV'jQ{0:X}Qj, Vh1);KOOV'jQ{0:X8}Qj, Vh1);jKKOOV{CVNVjhex.KOOjVP hexVyVh1.ToVO(jQX8Qj);jKKOOV{CVN from hexVjVzeger.KOOjViVh2VyVz.Parse(hex, NumberStyles.AllowHexSpecifier);KOOV'Vh1VxVh2);KO}K}KKjKK2af3K00002af3K2AF3K00002AF3KTruejSubstitution markers:jKK{0}K{1}K{2}jKKVDVQVbVJVAK{KOV%O{KOOV'jQ{0} {2}Qj, QxQ);KO}K}KKjKKUnhandled V+: VQ.FormatV+:KOVT (zero based) must be gV_r than or equalVjzeroVVlessKOthan the size of the argument Vn.jKKVDVQ;KV!KVJVAK{KOV?VP jMVg1j()KO{jKOOV{Use a V|mat VPVjcV_ the complete VP.KOOjVK 100.jToVOj(jQ0.0 MBQj);KO}KKOV?VP jMVg2j()KO{jKOOV{Use a V|mat VPVVthen concatenate.KOOjVK 100.jToVOj(jQ0.0Qj)V}jQ MBQj;KO}KKOconst Vi_maxVy1000000;KOV%O{KOOvar s1VyV,.V`New();KOOVw(ViiVy0; i < _max; i++)KOO{KOOOMVg1();KOO}KOOs1V3;KOOvar s2VyV,.V`New();KOOVw(ViiVy0; i < _max; i++)KOO{KOOOMVg2();KOO}KOOs2V3;KOOV'(V0(s1.V# * 1000 * 1000) /KOOO_max).ToVO(Q0.00 nsQ));KOOV'(V0(s2.V# * 1000 * 1000) /KOOO_max).ToVO(Q0.00 nsQ));KOOV5.Vu();KO}K}KKjKKj228.05 nsjOFormat VPKj241.35 nsjOFormat VPVVconcatjKKVDVQVbVJVAK{KOV%O{KOOViVhVy100;jKOOV{VO Vzerpolation can be used instead of a V|mat VP.KOOjV'j$QThe size is {Vh}.Qj);KO}K}KKjKKThe size is 100.j

(]iij>3ndstring.Formatstring.Format with numberstring.Format for paddingstring.Format and ToStringhandles hex number conversionsthrows an exceptionToString method, format stringstring interpolation