FUNDAMEN TA L TYPES: STRING S, A RRAY S, A ND ENUM S in C#.NET

Creating Data Matrix 2d barcode in C#.NET FUNDAMEN TA L TYPES: STRING S, A RRAY S, A ND ENUM S

CHAPTER 5 FUNDAMEN TA L TYPES: STRING S, A RRAY S, A ND ENUM S
Drawing ECC200 In C#
Using Barcode encoder for VS .NET Control to generate, create Data Matrix ECC200 image in .NET applications.
www.OnBarcode.com
Data Matrix 2d Barcode Scanner In Visual C#
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
output, as well as date, time, and currency output, which is dependent on the locale. The following sections provide detailed examples.
Creating Code 3/9 In Visual C#.NET
Using Barcode printer for .NET framework Control to generate, create Code-39 image in .NET applications.
www.OnBarcode.com
QR Code Drawer In C#
Using Barcode printer for Visual Studio .NET Control to generate, create Quick Response Code image in .NET applications.
www.OnBarcode.com
The Width Field (or Alignment Specifier)
Barcode Maker In Visual C#
Using Barcode generation for VS .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
GTIN - 12 Drawer In C#.NET
Using Barcode generator for Visual Studio .NET Control to generate, create UPCA image in .NET applications.
www.OnBarcode.com
Listing 5-6 provides some examples of formatting using the width field, including a negative width indicating left justification, and a currency formatting string the c2 following the colon in the Price column, which is ignored when used with a string. Listing 5-6. Formatting Strings Using the Width Field // string_alignment_specifier.cpp using namespace System; int main() { // The format string is interpreted as follows: // { 0, -30 } 30 characters in width, left-justified. // { 1, 10 } 10 characters in width, right-justified. // { 2, 10:c2 } 10 characters in width, currency with 2 decimal places. String^ format = "{0,-30}{1,10}{2,10:c2}"; String^ header = String::Format(format, "Item", "Quantity", "Price"); String^ str1 = str1->Format(format, "Matches, Strike Anywhere", 10, 0.99); String^ str2 = str2->Format(format, "Gloves", 1, 12.50); String^ str3 = str3->Format(format, "Iodine", 1, 4.99); Console::WriteLine(header); Console::WriteLine(str1 + "\n" + str2 + "\n" + str3); } The output of Listing 5-6 on U.S. English systems is as follows: Item Matches, Strike Anywhere Gloves Iodine Quantity 10 1 1 Price $0.99 $12.50 $4.99
Create Matrix Barcode In Visual C#.NET
Using Barcode drawer for .NET Control to generate, create 2D Barcode image in VS .NET applications.
www.OnBarcode.com
Code11 Drawer In Visual C#
Using Barcode creator for .NET Control to generate, create USD8 image in Visual Studio .NET applications.
www.OnBarcode.com
Numeric String Formatting
Reading Data Matrix In VB.NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Data Matrix ECC200 Generation In Java
Using Barcode generation for Java Control to generate, create ECC200 image in Java applications.
www.OnBarcode.com
Formatting in C runtime functions such as printf involves the use of formatting characters for various data types and, in particular, certain formatting characters for decimal or hexadecimal output, exponential format, and so on. The usual numeric formatting characters from C are supported, as well as additional formats for currency, and a special round-trip format specifically to ensure accurate results when reading the data back in using the Read or ReadLine methods. The code in Listing 5-7 shows the typical use of these formats. The formatting specifier follows the colon after the index (and optional alignment specifier specifying the width of the field) in the format string. In the following example, the alignment specifier is not used, and the index is always zero since we only have one variable to format.
Draw QR Code In None
Using Barcode encoder for Font Control to generate, create QR Code 2d barcode image in Font applications.
www.OnBarcode.com
UPCA Drawer In Java
Using Barcode generator for Java Control to generate, create UPCA image in Java applications.
www.OnBarcode.com
C HA PTER 5 FUNDA MENTAL TYPES : STRINGS , ARRA YS , AN D E NUMS
GS1 - 13 Creation In Objective-C
Using Barcode maker for iPad Control to generate, create UPC - 13 image in iPad applications.
www.OnBarcode.com
USS Code 128 Printer In None
Using Barcode printer for Software Control to generate, create Code 128 Code Set A image in Software applications.
www.OnBarcode.com
Listing 5-7. Formatting Numeric Strings // string_numerical_formatting.cpp using namespace System; int main() { String^ str; int i = -73000; double dbl = 1005.01; // formats for floating-point types: str = String::Format("Currency format: {0:c2}", dbl); Console::WriteLine(str); str = String::Format("Scientific format: {0:e6}", dbl); Console::WriteLine(str); str = String::Format("Fixed-point format: {0:f6}", dbl); Console::WriteLine(str); str = String::Format("General format: {0:g6}", dbl); Console::WriteLine(str); str = String::Format("Number format: {0:n6}", dbl); Console::WriteLine(str); str = String::Format("Percent format: {0:p6}", dbl); Console::WriteLine(str); str = String::Format("Round-trip format: {0:r6}", dbl); Console::WriteLine(str); // formats for integral types: str = String::Format("Decimal format: {0:d6}", i); Console::WriteLine(str); str = String::Format("General format: {0:g6}", i); Console::WriteLine(str); str = String::Format("Number format: {0:n0}", i); Console::WriteLine(str); str = String::Format("Hexadecimal format: {0:x8}", i); Console::WriteLine(str); }
Painting PDF-417 2d Barcode In None
Using Barcode creation for Microsoft Excel Control to generate, create PDF417 image in Excel applications.
www.OnBarcode.com
Generate Barcode In VB.NET
Using Barcode creation for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 5 FUNDAMEN TA L TYPES: STRING S, A RRAY S, A ND ENUM S
EAN / UCC - 13 Drawer In None
Using Barcode drawer for Online Control to generate, create USS-128 image in Online applications.
www.OnBarcode.com
GS1-128 Reader In VB.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Here is the output of Listing 5-7: Currency format: $1,005.01 Scientific format: 1.005010e+003 Fixed-point format: 1005.010000 General format: 1005.01 Number format: 1,005.010000 Percent format: 100,501.000000 % Round-trip format: 1005.01 Decimal format: -073000 General format: -73000 Number format: -73,000 Hexadecimal format: fffee2d8
QR Code Generation In VB.NET
Using Barcode creator for Visual Studio .NET Control to generate, create QR image in .NET applications.
www.OnBarcode.com
Generate Code 128 In None
Using Barcode generation for Word Control to generate, create Code128 image in Word applications.
www.OnBarcode.com
StringBuilder
Because String represents an immutable string, for manipulation and editing of strings in-place, you need to use StringBuilder rather than String. StringBuilder contains methods for appending, inserting, removing, and replacing elements of a string (see Listing 5-8). StringBuilder maintains an internal buffer with a given capacity and expands this capacity as the size of the string increases. Listing 5-8. Using StringBuilder // stringbuilder.cpp using namespace System; using namespace System::Text; int main() { // Construct a StringBuilder string with initial contents // "C" and initial capacity 30. StringBuilder^ sb = gcnew StringBuilder("C", 30); sb->Append(gcnew array<Char>{'+','+'}); sb->Append("/CLI."); sb->Insert(0, "I love "); sb->Replace(".","!"); Console::WriteLine( sb->ToString() ); } The output of Listing 5-8 is as follows:
C HA PTER 5 FUNDA MENTAL TYPES : STRINGS , ARRA YS , AN D E NUMS
I love C++/CLI!
Refer to the documentation for the .NET Framework or CLI Base Class Library for further information.
Conversions Between Strings and Other Data Types
You ve seen many examples of rendering primitive types as strings. What about converting from a string to a primitive type System::String implements IConvertible, which means it supports conversions to a variety of types using functions such as ToBoolean, ToInt32, and so on. Also, the object wrappers for the primitive types support the ability to parse strings. There are static methods called Parse on the classes for the primitive types that take a string to be parsed as a parameter and return an object of the numeric type. Listing 5-9 provides some examples. Listing 5-9. Converting Strings to Primitive Types // convert_and_parse.cpp using namespace System; int main() { String^ str1 = "115"; String^ str2 = "1.4e-12"; // Parse the string to get the integer value. int i = Int32::Parse(str1); // Get the double value. double x = Double::Parse(str2); // Use Convert class to convert the value. int j = Convert::ToInt32(str1); double y = Convert::ToDouble(str2); // Exception handlers may be used to catch parse failures and overflows. try { int k = Int32::Parse("bad format"); } catch(FormatException^ e) { Console::WriteLine("Exception occurred! {0}", e->Message ); } }
Copyright © OnBarcode.com . All rights reserved.