Chapter 3: 'Strings and Regular Expressions'

This book provides practical examples for virtually every aspect of the C# programming language. It is structured for progressive learning, so it can be read cover-to-cover or used as a comprehensive reference guide. You will be exposed to everything from low-level information on the Garbage Collector to advanced concepts, such as creating applications that use Enterprise Services, creating Web Services, and even advanced Windows GUI.


Download chapter 3: 'Strings and Regular Expressions'


Excerpted from the book "Microsoft Visual C# 2005 Unleashed," ISBN 0672327767, Copyright 2006. Written permission from SAMS Publishing is required for all other uses. All rights reserved. 800 East 96th Street, Indianapolis, Indiana 46240.






Chapter Excerpt:


Formatting Strings

One of the most common tasks when working with strings is formatting them. When displaying information to users, you often display things like dates, times, numeric values, decimal values, monetary values, or even things like hexadecimal numbers. C# strings all have the ability to display these types of information and much more. Another powerful feature is that when you use the standard formatting tools, the output of the formatting will be localization-aware. For example, if you display the current date in short form to a user in England, the current date in short form will appear different to a user in the United States.

To create a formatted string, all you have to do is invoke the Format method of the string class and pass it a format string, as shown in the following code:

string formatted = string.Format("The value is {0}", value);

The {0} placeholder indicates where a value should be inserted. In addition to specifying where a value should be inserted, you can also specify the format for the value.

Other data types also support being converted into strings via custom format specifiers, such as the DateTime data type, which can produce a custom-formatted output using

DateTime.ToString("format specifiers");

Table 3.1 illustrates some of the most commonly used format strings for formatting dates, times, numeric values, and more.

Take a look at the following lines of code, which demonstrate using string format specifiers to create custom-formatted date and time strings:

DateTime dt = DateTime.Now;

Console.WriteLine(string.Format("Default format: {0}", dt.ToString()));
Console.WriteLine(dt.ToString("dddd dd MMMM, yyyy g"));
Console.WriteLine(string.Format("Custom Format 1: {0:MM/dd/yy hh:mm:sstt}", dt));
Console.WriteLine(string.Format("Custom Format 2: {0:hh:mm:sstt G\MT zz}", dt));

Here is the output from the preceding code:

Default format: 9/24/2005 12:59:49 PM
Saturday 24 September, 2005 A.D.
Custom Format 1: 09/24/05 12:59:49PM
Custom Format 2: 12:59:49PM GMT -06

You can also provide custom format specifiers for numeric values as well. Table 3.2 describes the custom format specifiers available for numeric values.

If multiple format sections are defined, conditional behavior can be implemented for even more fine-grained control of the numeric formatting:

  • Two sections—If you have two formatting sections, the first section applies to all positive (including 0) values. The second section applies to negative values. This is extremely handy when you want to enclose negative values in parentheses as is done in many accounting software packages.
  • Three sections—If you have three formatting sections, the first section applies to all positive (not including 0) values. The second section applies to negative values, and the third section applies to zero.

The following few lines of code illustrate how to use custom numeric format specifiers.

double dVal = 59.99;
double dNeg = -569.99;
double zeroVal = 0.0;
double pct = 0.23;

string formatString = "{0:$#,###0.00;($#,###0.00);nuttin}";
Console.WriteLine(string.Format(formatString, dVal));
Console.WriteLine(string.Format(formatString, dNeg));
Console.WriteLine(string.Format(formatString, zeroVal));
Console.WriteLine(pct.ToString("00%"));

The output generated by the preceding code is shown in the following code:

$59.99 ($569.99) nuttin 23%

Chapter 3: 'Strings and Regular Expressions'

Visit the SAMS Publishing website for a detailed description and to learn how to purchase this title.

This was first published in May 2007

Dig deeper on SAP interface technologies

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close