Visual C#. NET 2003 Unleashed
|
The .NET Framework finally brings a unified string definition to the multiple languages targeted at .NET. A string, as far as the Common Type System (CTS) is concerned, is just an array of Unicode characters. The .NET String class provides several methods that allow for easy comparison, concatenation, formatting, and general manipulation of strings. Understanding the Immutability of Strings
The String class in .NET is immutable; in other words, the string itself is never modified. When characters or other strings are appended to a string, a new string is created as a result. The original string and the string to append are used to generate an entirely new string. Such immutability can cause a performance degradation for applications in which heavy string manipulation is performed. To avoid this reduction in performance, you should make use of the StringBuilder class, which is discussed in the following section, for all heavy-duty string manipulation. TIP Actually, there are very few cases in which you would not want to use a StringBuilder. As a general rule, if you perform more than one string concatenation within a scope block (method, for loop, and so on), or even a single very large concatenation, you should remove the standard concatenation and replace it with the use of a StringBuilder.
Applying Formatting to Strings
After declaring a string, the next task is to format data for presentation. This area of string formatting is not well documented and few examples exist to fully illustrate how rich the string-formatting features are within .NET. Basic string formatting allows for data to be inserted to locations within a string. These insertion locations are denoted by using placeholders along with an ordinal value that corresponds to the sequence of the item to be inserted. For example, consider inserting an integer value within a string Listing 4.1. Simple String Formatting Example
using System; namespace Listing_4_1 { class Class1 { static void Main(string[] args) { int a = 1; int b = 2; int c = 3; Each placeholder represents the zero-based index of the item in the argument list to insert into the string. This is the most basic type of formatting available and also the most often used. There also exists the ability to align values either left or right within a padded region of the insertion point. The padding ensures that the width of the inserted item with is at least N character spaces wide and the alignment determines whether the inserted string is aligned to the left or right of the area. Listing 4.2 demonstrates how to make use of padding and alignment Listing 4.2. Padding and Alignment in String Formatting
using System; namespace Listing_4_2 { class Class1 { static void Main(string[] args) { Beyond basic insertion and padding of values into a string, string formatting also offers the ability to format data such as currency, dates, and hexadecimal values. The list of formatting options can be separated into two categories: basic and custom. Basic and custom formatting applies to both integer values and date values. Tables 4.1 through 4.4 list the formatting for integers and dates for both basic and custom formatting.
With the exception of the group separator, custom integer formatting is obvious at first glance. The group separator allows for multiple format options based on the integer value to be formatted. Essentially, the group separator allows for three different format specifications, based on the value of the integer to be formatted. Those specifications apply to a positive value, and then a negative value, and finally a zero value. For instance, if you want negative floating point values to appear in parentheses, the following formatting could be used: string result = string.Format("{0:$##,###.00;$(##,###.00);$-.--}", amount);
The next common data type for formatting is the DateTime struct within .NET. There are many options when it comes to formatting dates, and Tables 4.3 and 4.4 list the various formatting specifiers and outcomes for date formatting.
Using Escape Sequences
It is often necessary to include in a string special characters such as tab, newline, or even the \ character. To insert such formatting, it is necessary to use the escape character (\), which tells the formatting parser to treat the next character as a literal character to be inserted into the resulting string. To insert the escape character, it is necessary to escape it with the escape character. The following code illustrates this: string escapeMe = string.Format( "C:\\SAMS\\Code" ); With the escape character in place, the value of escapeMe is "C:\SAMS\CODE".
Locating Substrings
One of the most common string-processing requirements is the locating of substrings within a string. The System.String class provides several methods for locating substrings and each method in turn provides several overloaded versions of itself. Table 4.5 details the methods for locating substrings.
Adding Padding
Just as with format specifiers and padding, the String class provides a set of padding methods that pad a string with a space or specified character. Padding can be used to pad spaces or characters to the left or right of the target string. The code in Listing 4.3 shows how to pad a string to 20 characters in length with leading spaces. Listing 4.3. 20 Characters Wide String Left Padded with Spaces
string leftPadded = "Left Padded"; Console.WriteLine("123456789*123456789*123456789*"); Console.WriteLine( leftPadded.PadLeft(20, ' ' ) );
The output of the code in Listing 4.3 is as follows: 123456789*123456789* Left Padded
Trimming Characters
Sometimes it is necessary to remove characters from a string and this is the purpose of trimming. The TRim method allows for the removal of spaces or characters from either the start or end of a string. By default, the trim method removes leading and trailing spaces from a string. In addition, there are two other trimming methods. trimStart removes spaces or a list of specified characters from the beginning of a string. TRimEnd removes spaces or a list of specified characters from the end of a string. You can access the trim method and others like it on any string variable, as shown here: string myTrimmedString = myString.Trim();
Replacing Characters
To replace characters or substrings in a string, use the Replace method. For instance, to remove display formatting from a phone number such as (919) 555-1212, the following code can be used: string phoneNumber = "(919) 555-1212"; string fixedPhoneNumber = phoneNumber.Replace( "(", "" ). Replace( ")", "" ).Replace( "-", "" ) .Replace( " ", "" ); Console.WriteLine( fixedPhoneNumber );
Notice how the Replace method is used. Each time Replace is called, a new string is created. Thus, the cascading use of the Replace method to remove all unwanted strings is necessary.
Splitting Strings
String splitting comes in handy for parsing comma-separated values or any other string with noted separated characters. The Split method requires nothing more than a character parameter that denotes how to split up the string. The result of this operation is an array of strings where each element is a substring of the original string. To separate or spilt a comma-separated list such as apple,orange,banana, merely invoke the Split method passing in the comma as the split token. The following code demonstrates the result: string fruit = "apple,orange,banana"; string[] fruits= fruit.Split( ',' ); foreach (string fruitName in fruits) Console.WriteLine(fruitName); //Result //fruits[0] -> apple //fruits[1] -> orange //fruits[2] -> banana
Modifying Case
The last two major methods of the String class involve changing the case of a string. The case can be changed to uppercase or lowercase and results in a new string of the specified case. Remember that strings are immutable and any action that modifies a string results in a new string. Therefore, the following takes place: string attemptToUpper = "attempt to upper"; attemptToUpper.ToUpper( ); //attemptToUpper is still all lower case
To see the effect of the ToUpper() method, the result string has to be assigned to a variable. The following illustrates the proper use of ToUpper(): string allLower = "all lower"; string ALL_UPPER = allLower.ToUpper( ); //ALL_UPPER -< "ALL LOWER";
The StringBuilder
To improve performance, the StringBuilder class is designed to manage an array of characters via direct manipulation. Such an implementation eliminates the need to constantly allocate new strings. This improves performance by saving the garbage collector from tracking and reclaiming small chunks of memory, as would be the case using standard string functions and concatenation. The StringBuilder class is located in the System.Text namespace. Appending Values
The most basic use of the StringBuilder class is to perform string concatenation, which is the process of building a result string from various other strings and values until the final string is complete. The StringBuilder class provides an Append method. The Append method is used to append values to the end of the current string. Values can be integer, boolean, char, string, DateTime, and a list of others. In fact, the Append method has 19 overloads in order to accommodate any value you need to append to a string. Using AppendFormat
In addition to appending values to the current string, StringBuilder also provides the ability to append formatted strings. The format specifiers are the same specifiers listed in the previous section. The AppendFormat method is provided in order to avoid calls to string.Format(...) and the unnecessary creation of additional strings. Inserting Strings
The insertion of strings is another useful method provided by the StringBuilder class. The Insert method takes two parameters. The first parameter specifies the zero-based index at which to begin the insertion. The second parameter is the value to insert at the specified location. Similar to the Append method, the Insert method provides 18 different overloads in order to support various data types for insertion into the string. Listing 4.4 shows the usage of the Insert method. Listing 4.4. Using the Insert Method to Create a SQL Statement
using System; using System.Text; namespace Listing_4_4 { class Class1 { [STAThread] static void Main(string[] args) { StringBuilder stmtBuilder = new StringBuilder( "SELECT FROM MYTABLE" ); Console.Write( "Enter Columns to select from MYTABLE: "); string columns = Console.ReadLine( ); //FirstName, LastName stmtBuilder.Insert( 7, columns ); //insert a space after the column names stmtBuilder.Insert( 7 + columns.Length, " " ); //SELECT FirstName, LastName FROM MYTABLE Console.WriteLine( stmtBuilder.ToString( ) ); } } } Replacing Strings and Characters
You might run across a need to generate strings based on templates where certain tokens (substrings) are later replaced with values. In fact, this is how Visual Studio .NET works. There is a template file from which each project is created. The new source file that is created is generated from a template and various tokens are replaced based on the type of project, the name of the project, and other options. You can achieve this same effect using the Replace method provided by StringBuilder. Using the Replace method, it is possible to create template strings, such as SQL statements, and replace tokens with actual values as demonstrated by the following code: StringBuilder selectStmtTemplate = string StringBuilder(); selectStmtTemplate.Append( "SELECT $FIELDS FROM $TABLE" ); selectStmtTemplate.Replace( "$FIELDS", fieldList ); selectStmtTemplate.Replace( "$TABLE", tableName );
Removing Substrings
The Remove method allows for sections of the underlying string to be completely removed from the StringBuilder object. The Remove method takes two parameters. The first parameter specifies the zero-based index of the position denoting the starting point. The second parameter specifies the length or number of characters to remove. |
|