Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать

540

Extreme C#

PART IV

Console.WriteLine( “myStringBuilder[1]: {0}”, charResult);

The return value in this case is ‘y’.

String Formatting

When performing a Console.WriteLine() method call, any format strings default to string type unless special formatting is applied. Often it’s necessary to perform more sophisticated formatting on various types such as numbers, strings, and dates.

Numeric Formatting

C# has several formatting characters for numeric formatting. Table 25.1 shows the C# number format specifiers.

TABLE 25.1 Numeric Format Specifiers

Format Character

Description

C or c

Currency

D or d

Decimal

E or e

Scientific/exponential

F or f

Fixed point

G or g

General (can be E or F format)

N or n

Number

R or r

Roundtrip (convertible to string and back)

X or x

Hexadecimal

 

 

The following example shows how to use numeric formatting:

Console.WriteLine(“Hex: {0:x}”, 255);

This example converts the integer 255 to hexadecimal notation. The result is hex ff. The value is in lowercase because a lowercase format character was used. Results are in uppercase when uppercase format characters are used.

String Manipulation

CHAPTER 25

541

Picture Formatting

It’s often necessary to have more control over the format of output beyond default formatting or a simple numeric formatting character. In these cases, picture formatting will help present output exactly as desired. Table 25.2 shows the picture formatting characters.

TABLE 25.2 Picture Format Characters

Format Character

Description

0

Zero placeholder

#

Display digit placeholder

.

Decimal point

,

Group separator/multiplier

%

Percent

E+0, E-0, e+0, e-0

Exponent notation

\

Literal character

‘ABC’ or “ABC”

Literal string

;

Section separator

 

 

The following example shows how to use picture formatting:

Console.WriteLine(“Million: {0:$#,#.00}”, 1000000);

This example formats the number to make it appear as currency. (The C number format character could have been used, but it wouldn’t have served the purpose of this example.) The result of this formatting produces “$1,000,000.00”. The $ sign is placed into the output at the position it appears. The # symbol holds a place for a number before and after the comma. The ‘,’ character causes a comma to be placed between every three digits of the output. The decimal point will be placed to the right of the whole number. To get the cents portion to appear, two zeros are put after the decimal point in the format specifier. Had these been # symbols, nothing would have appeared after the decimal point.

Regular Expressions

Regular expressions provide the capability to manipulate and search text efficiently. The System.Text.RegularExpressions namespace contains a set of classes that enable regular expression operations in C# programs. Listing 25.1 shows the code for a program similar to grep (Global Regular Expression Print) expressions:

25

TRINGS

ANIPULATIONM

542

Extreme C#

PART IV

LISTING 25.1 Regular Expressions

using System;

using System.Text.RegularExpressions; using System.IO;

class lrep

{

static int Main(string[] args)

{

if (args.Length < 2)

{

Console.WriteLine(“Wrong number of args!”); return 1;

}

Regex re = new Regex(args[0]);

StreamReader sr = new StreamReader(args[1]);

string nextLine = sr.ReadLine();

while (nextLine != null)

{

Match myMatch = re.Match(nextLine);

if (myMatch.Success)

{

Console.WriteLine(“{0}: {1}”, args[1], nextLine);

}

nextLine = sr.ReadLine();

}

sr.Close();

return 0;

}

}

Note

Global Regular Expression Print (grep), written by Doug McIlroy, is a popular Unix utility. It allows you to perform a command line search for regular expressions within the text of one or more files.

String Manipulation

CHAPTER 25

543

The Listing 25.1 program is called lrep, which stands for Limited Regular Expression Print. It may be limited in features, but because of the built-in regular expression classes, it’s very powerful. Here’s an example of how to use it:

lrep string lrep.cs

The first parameter, lrep, is the command name of the program. The second parameter, string, is the regular expression. It happens to be a normal string without anything special, but can also take the same set of regular expressions as the Perl programming language. The third parameter, lrep.cs, is the filename to search for the regular expression. Here’s the output:

lrep.cs:

static int

Main(string[] args)

lrep.cs:

string

nextLine = sr.ReadLine();

Each line of output contains the name of the file that was searched. Following that is the text of the line where the regular expression matched. The next example shows how the regular expression is set in the program:

Regex re = new Regex(args[0]);

A regular expression is created by instantiating a Regex object. The following example shows one way to use a regular expression object:

Match myMatch = re.Match(nextLine);

The Match() method of the Regex class is used to determine if a given string contains text that matches a regular expression. This program opens a file, specified in the command line arguments, and reads each line to see if there is a match. By using the Success property of the match object, the program can figure out that a match was made. This program writes the positive matching lines to the console, as shown in the output lines.

This program used a late bound matching scheme to achieve its goals. However, a regular expression may be initialized with constants, increasing efficiency through compile time optimization.

Summary

There is a plethora of options available in the way of string manipulation with the system libraries. The String class provides basic string handling but has many methods available for returning new strings with various modifications.

For sophisticated string manipulation, use the StringBuilder class. It allows modification of the string in the same object without the overhead of creating a new object with each operation.

25

TRINGS

ANIPULATIONM

544

Extreme C#

PART IV

Strings need to be formatted for many processing activities. There are simple number formatting options as well as picture formatting.

A welcome feature of the system libraries is regular expressions. Regular expressions allow powerful string manipulation that is more efficient than either String or StringBuilder class operations.

Strings and StringBuilders have various features that make them work well as collection objects. The next chapter, “C# Collections,” explains why this is true and provides insight to help understand the internal mechanism of collections.

CHAPTER 28

Reflection

IN THIS CHAPTER

• Discovering Program Information 582

• Dynamically Activating Code 588

Reflection.Emit 590