c# net qr code generator Composite Formatting with String.Format in Visual C#.NET

Encode QR Code in Visual C#.NET Composite Formatting with String.Format

Composite Formatting with String.Format
Paint QR Code JIS X 0510 In Visual C#.NET
Using Barcode drawer for .NET Control to generate, create QR Code image in VS .NET applications.
www.OnBarcode.com
QR Reader In Visual C#
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
The previous examples have all turned exactly one piece of information into a single string (or vice versa). Very often, though, we need to compose multiple pieces of information into our final output string, with different conversions for each part. We could do that by composing strings (something we ll look at later in this chapter), but it is often more convenient to use a helper method: String.Format. Example 10-44 shows a basic example.
2D Encoder In Visual C#.NET
Using Barcode generator for .NET framework Control to generate, create Matrix Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Make UPC - 13 In Visual C#
Using Barcode generator for VS .NET Control to generate, create EAN-13 image in .NET applications.
www.OnBarcode.com
int val1 = 32; double val2 = 123.457; DateTime val3 = new DateTime(1999, 11, 1, 17, 22, 25); string formattedString = String.Format("Val1: {0}, Val2: {1}, Val3: {2}", val1, val2, val3); Console.WriteLine(formattedString);
UPC A Drawer In Visual C#
Using Barcode generator for VS .NET Control to generate, create GS1 - 12 image in Visual Studio .NET applications.
www.OnBarcode.com
Creating ECC200 In C#.NET
Using Barcode maker for .NET Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
This method takes a format string, plus a variable number of additional parameters. Those additional parameters are substituted into the format string where indicated by a format item. At its simplest, a format item is just an index into the additional parameter array, enclosed in braces (e.g., {0}). The preceding code will therefore produce the following output:
Barcode Generator In Visual C#
Using Barcode encoder for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
2 Of 7 Code Creator In C#
Using Barcode drawer for .NET Control to generate, create Codabar image in VS .NET applications.
www.OnBarcode.com
Val1: 32, Val2: 123.457, Val3: 01/11/1999 17:22:25
QR Code JIS X 0510 Generator In None
Using Barcode printer for Office Excel Control to generate, create Quick Response Code image in Excel applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Printer In None
Using Barcode generation for Software Control to generate, create QR Code image in Software applications.
www.OnBarcode.com
A specific format item can be referenced multiple times, and in any order in the format string. You can also apply the standard and custom formatting we discussed earlier to any of the individual format items. Example 10-45 shows that in action.
Encode PDF417 In None
Using Barcode generator for Microsoft Word Control to generate, create PDF 417 image in Word applications.
www.OnBarcode.com
QR-Code Creation In .NET Framework
Using Barcode printer for VS .NET Control to generate, create QR Code ISO/IEC18004 image in .NET applications.
www.OnBarcode.com
int first = 32; double second = 123.457; DateTime third = new DateTime(1999, 11, 1, 17, 22, 25);
GS1-128 Reader In Visual Basic .NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
European Article Number 13 Drawer In Objective-C
Using Barcode maker for iPad Control to generate, create EAN13 image in iPad applications.
www.OnBarcode.com
string output = String.Format( "Date: {2:d}, Time: {2:t}, Val1: {0}, Val2: {1:#.##}", first, second, third); Console.WriteLine(output);
PDF 417 Creation In .NET
Using Barcode maker for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
Draw QR Code JIS X 0510 In Visual Studio .NET
Using Barcode generation for Reporting Service Control to generate, create Quick Response Code image in Reporting Service applications.
www.OnBarcode.com
Notice the colon after the index, followed by the simple or custom formatting string, which transforms the output:
Scanning Universal Product Code Version A In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Create GTIN - 13 In Java
Using Barcode generation for Java Control to generate, create EAN13 image in Java applications.
www.OnBarcode.com
Date: 01/11/1999, Time: 17:22, Val1: 32, Val2: 123.46
UPC Symbol Maker In Java
Using Barcode printer for Java Control to generate, create UPC Symbol image in Java applications.
www.OnBarcode.com
Create EAN13 In VS .NET
Using Barcode creator for VS .NET Control to generate, create EAN13 image in VS .NET applications.
www.OnBarcode.com
String.Format is a very powerful technique, but you should be aware that there is some
overhead in its use with value types. The additional parameters take the form of an array of objects (so that we can pass in any type for each format item). This means that the values passed in are boxed, and then unboxed. For many applications this overhead will be irrelevant, but, as always, you should measure and be aware of the hidden cost.
Culture Sensitivity
Up to this point, we ve quietly ignored a significantly complicating factor in string manipulation: the fact that the rules for text vary considerably among cultures. There are also lots of different types of rules in operation, from the characters to use for particular types of separators, to the natural sorting order for characters and strings. I ve already called out an example where the output on my UK English machine was different from that on a U.S. English computer. As another very simple example, the decimal number we write as 1.8 in U.S. or UK English would be written 1,8 in French. For the .NET Framework, these rules are encapsulated in an object of the type System.Globalization.CultureInfo. The CultureInfo class makes certain commonly used cultures accessible through static properties. CurrentCulture returns the default culture, used by all the culture-sensitive methods if you don t supply a specific culture to a suitable overload. This value can be controlled on a per-thread basis, and defaults to the Windows default user locale. Another per-thread value is the CurrentUICulture. By default, this is based on the current user s personally selected preferred language, falling back on the operating system default if the user hasn t selected anything. This culture determines which resources the system uses when looking up localized resources such as strings.
CurrentCulture and CurrentUICulture may sound very similar, but are often different. For example, Microsoft does not provide a version of Windows translated into British English Windows offers British users Favorites and Colors despite a national tendency to spell those words as Favourites and Colours. But we do have the option to ask for UK conventions for dates and currency, in which case CurrentCul ture and CurrentUICulture will be British English and U.S. English, respectively.
Finally, it s sometimes useful to ensure that your code always behaves the same way, regardless of the user s culture settings. For example, if you re formatting (or parsing) text for persistent storage, you might need to read the text on a machine configured for a culture other than that on which it was created, and you will want to ensure that it is interpreted correctly. If you rely on the current culture, dates written out on a UK machine will be processed incorrectly on U.S. machines because the month and day are reversed. (In the UK, 3/12/2010 is a date in December.) The InvariantCulture property returns a culture with rules which will not vary with different installed or userselected cultures.
If you ve been looking at the IntelliSense as we ve been building the string format examples in this chapter, you might have noticed that none of the obviously culture-sensitive methods seem to offer an overload which takes a CultureInfo. However, on closer examination, you ll notice that CultureInfo also implements the IFormatProvider interface. All of the formatting methods we ve looked at do provide an overload which takes an instance of an object which implements IFormatProvider. Problem solved!
You can also create a CultureInfo object for a specific culture, by providing that culture s canonical name to the CreateSpecificCulture method on the CultureInfo object. But what are the canonical names You may have come across some of them in the past. UK English, for instance, is en-GB, and French is fr. Example 10-46 gets a list of all the known canonical names by calling another method on CultureInfo that lists all the cultures the system knows about: GetCultures.
var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures). OrderBy(c => c.EnglishName); foreach (var culture in cultures) { Console.WriteLine("{0} : {1}", culture.EnglishName, culture.Name); }
We won t reproduce the output here, because it is a bit long. This is a short excerpt:
English (United Kingdom) : en-GB English (United States) : en-US English (Zimbabwe) : en-ZW Estonian : et Estonian (Estonia) : et-EE Faroese : fo Faroese (Faroe Islands) : fo-FO Filipino : fil Filipino (Philippines) : fil-PH Finnish : fi Finnish (Finland) : fi-FI French : fr
Notice that we re showing the English version of the name, followed by the canonical name for the culture. Example 10-47 illustrates a difference in string formatting between two different cultures.
CultureInfo englishUS = CultureInfo.CreateSpecificCulture("en-US"); CultureInfo french = CultureInfo.CreateSpecificCulture("fr"); double value = 1.8; Console.WriteLine(value.ToString(englishUS)); Console.WriteLine(value.ToString(french));
This will produce the output we d expect:
1.8 1,8
Copyright © OnBarcode.com . All rights reserved.