Wednesday, September 8, 2010

The StringBuilder and String classes provide ways to perform string manipulation. If your program requires many manipulations, then the StringBuilder class will likely be more efficient than using the String class. The String class provides an immutable object, which means that once the value of the string instance is set, it cannot be changed. Even though it appears that the application is changing the value of the string instance, it is actually returning a new instance of the String class in memory. For this reason, the StringBuilder class will likely cause less overhead in your application. This set of samples shows some basic manipulation using both the StringBuilder and String classes.


Contents:
The following examples will show how to use the StringBuilder class to append, insert, and replace text within a StringBuilder instance. It will also show how to use the String class to copy, split, and find a substring within a given String instance.
The self-extracting archive contains all source code for this set of samples. The file contains the following directories:

• Code: Contains subfolders for all the samples (see descriptions below):
• BuilderAppend
• BuilderInsert
• BuilderReplace
• StringCopy
• StringSplit
• StringSubstring
• Doc: Contains all documentation, including descriptions of samples, installation instructions, and information on how to use the sample application.
• Misc: Contains any additional files required to run the sample application.


How to Use

Run the self-extracting archive to install the sample files. All of the examples here are separate console applications that can be opened in Visual Studio.NET and run accordingly.

Description


StringBuilder.Append
The Append method takes the specified argument and appends it to the end of the existing value held in a StringBuilder object. This example is a console application that asks the user to input his/her name. The application takes the name entered by the user and appends it to the greeting that is held in the StringBuilder object Greeting. The Append method of the StringBuilder class has 19 overloads. This example uses the Append method of the String object to append Name to the Greeting. The StringBuilder Greeting is initialized with no value, and the value Good Morning is appended to the string representation. Next, the name the user enters is appended to this greeting with the code:

Greeting.Append(", "+Name+"!");

StringBuilder.Insert
The Insert method takes a specified argument and inserts the value at the specified location within the existing value of the StringBuilder object. This example is a console application similar to the Append example. The application takes the name entered by the user and inserts it at the specified location. The Insert method has 18 overloads. This example uses the Insert method of the String object to insert the name you enter after the words "Good Morning" and before the "!" with the following code:

Greeting.Insert(12, ", "+Name);

The first argument is an integer value representing the location within the string to insert the value. The second argument is the value to insert into the existing string. If the integer value supplied is greater than the length of the string value contained within the StringBuilder object (for example, if the value 35 is entered here), an exception of type System.ArgumentOutOfRangeException will occur.

StringBuilder.Replace
The Replace method searches the existing string value and replaces all instances of the value with another specified value. This example builds on the Insert example by asking the user to also enter a nickname into the application. If the user has entered a name and a nickname, then the application will replace the original name with the nickname in the StringBuilder Greeting object with the following code:

Greeting.Replace(Name, Nickname);

String.Copy
The Copy method of the String class creates a new instance of a string with the same value as the string being copied. In this example the user enters some text, and that value is placed into a string object named inputText. The application copies this value into the new object named copiedText and appends some additional text in the following code:

string inputText = Console.ReadLine();
string copiedText = string.Copy(myText);
copiedText+=" more text";

Notice that the Copy method is a static method of the String class and therefore is called using the class name and not the instance name.

String.Split and String.Join
The Split method identifies a substring within the specified string delimited by one or more characters. The substring values are placed into a string array. In this example, the string instance textOriginal contains the value "Value 1, Value 2, Value 3". The application calls the Split method and initializes the textarray string array with the following code:

string[] textArray = text.Split(',');

The example then loops through all of the values in the array and writes them to the screen.
The Join method of the String class concatenates a specified character string within values of a string array and produces a single string. In this example, the textarray string array is re-joined using a ":" with the following code:

string newJoin = string.join(":", textArray);

Notice that the Join method is a static method of the String class and therefore is called using the class name and not the instance name.

String.Substring
The Substring method returns a string representation from a specified start point. This method has two overloads where one method also accepts a length argument. In this example, a string is extracted from the original string instance beginning at the 4th character and having a length of 3.

string Contained = originalString.Substring(4, 3).ToString();



No comments:

Post a Comment

Followers