DQbJA%{9hPhy(ihy.hyq.{Pg9|P9|PMyPhhh{aM'M(DQbJA%{P99hyPMyPPy'MPyDQbJA%{|P99Ej9EjP|y{PPyP|PyP|{a|P''DQbJA%ihyPyPh{PyhO{''DQDQbJA%ihy{aEp|'h'hb'h'h{NjPyhO{Njzihyz'hxhDQbJA%'+Q+T_jVnDQ!JA?Pg{|Pj_PKO?Pg{|PVKO}iy%y,`wiyg3y,`wiyg3'0#O'0#O5uPPVDQbJA%ihy{Oz|P'haAbbrEABDBEBtErBeX-~~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: microsoft.com `https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings`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.

Z[] [[[[[ { Z[Z{] ZZ[Declare three variables. ZZ[The [s they have are not important. ZZ][ [1[]"["]; ZZ[[2[10000; ZZ[ [3[[[(2015, 11, 1);] ZZ[Use [.Format m[ with four arguments. ZZ[The first argument is the [matting [. ZZ[It specifies how the next arguments are [matted. ZZ][ [[][.Format](]"{0}: {1:0.0} - {2:yyyy}"], ZZZ[1, ZZZ[2, ZZZ[3);] ZZ[[ the [. ZZ][[); Z} } ] [: 10000.0 - 2015] [[[[[ { Z[Z{] ZZ[Format a ratio as a percentage [. ZZ[You must specify the percentage symbol. ZZ[It will multiply the [ by 100. ZZ]double ratio[0.73; ZZ[ [[][.Format](]"[[{0:0.0%}"], ZZZratio); ZZ[[); Z} } ] [[73.0%] [[[[[ { Z[Z{] ZZ[The constant [matting [. ZZ[It specifies the padding. ZZ[A negative [ means[left-align. ZZ[A positive [ means[right-align. ZZ]const [ [mat[]"{0,-10} {1,10}"];] ZZ[Construct the [s. ZZ][ line1[][.Format]([mat, ZZZ100, ZZZ5); ZZ[ line2[][.Format]([mat, ZZZ"Carrot", ZZZ"Giraffe");] ZZ[[ the [matted [s. ZZ][line1); ZZ[line2); Z} } ] 100ZZZZ 5 CarrotZZGiraffe] [[[[[ { Z[Z{ ZZ[[[123; ZZ[ a[][.Format](]"{0:0000}"], [);] [Too complex. ZZ][ b[[.To[("0000");] [Simpler. ZZ][a); ZZ[b); Z} } ] 0123 0123] [[; [[.Globalization[[[ { Z[Z{ ZZ[[1[10995;] ZZ[[ [[hex [mat. ZZ][]"{0:x}"], [1); ZZ[]"{0:x8}"], [1)[ZZ[]"{0:X}"], [1); ZZ[]"{0:X8}"], [1);] ZZ[C[[hex. ZZ][ hex[[1.To[(]"X8"]);] ZZ[C[ from hex[[eger. ZZ][[2[[.Parse(hex, NumberStyles.AllowHexSpecifier); ZZ[[1[[2); Z} } ] 2af3 00002af3 2AF3 00002AF3 True]Substitution markers:] {0} {1} {2}] [[[[[ { Z[Z{ ZZ[]"{0} {2}"], "x"); Z} } ] Unhandled [: [.Format[: Z[ (zero based) must be g[r than or equal[zero[less Zthan the size of the argument [.] [[; [ [[ { Z[[ ]M[1]() Z{] ZZ[Use a [mat [[c[ the complete [. ZZ][ 100.]To[](]"0.0 MB"]); Z} Z[[ ]M[2]() Z{] ZZ[Use a [mat [[then concatenate. ZZ][ 100.]To[](]"0.0"])[]" MB"]; Z} Zconst [_max[1000000; Z[Z{ ZZvar s1[[.[New(); ZZ[([i[0; i < _max; i++) ZZ{ ZZZM[1(); ZZ} ZZs1[; ZZvar s2[[.[New(); ZZ[([i[0; i < _max; i++) ZZ{ ZZZM[2(); ZZ} ZZs2[; ZZ[([(s1.[ * 1000 * 1000) / ZZZ_max).To[("0.00 ns")); ZZ[([(s2.[ * 1000 * 1000) / ZZZ_max).To[("0.00 ns")); ZZ[.[(); Z} } ] ]228.05 ns]ZFormat [ ]241.35 ns]ZFormat [[concat] [[[[[ { Z[Z{ ZZ[[[100;] ZZ[[ [erpolation can be used instead of a [mat [. ZZ][]$"The size is {[}."]); Z} } ] The size is 100.]

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