Language: Strings

This sample demonstrates many of the methods of the String class provided by the .NET Framework. The sample form breaks the methods up into two three groups: methods that return strings (such as Insert, Remove, and so on), methods that return information (such as IndexOf), and shared methods (like String.Format). In addition, the demonstration introduces two useful adjuncts to string handling: the StringBuilder and StringWriter classes.

Featured Highlights

The sample form allows you to interactively investigate many of the methods of the String, StringBuilder, and StringWriter classes. The first three tabs (Methods that Return Strings, Methods that Return Information, Shared Methods) all include buttons that display parameters for each method. If you want to change a parameter and test the behavior, select Recalc when you're done, to update the results. You can also change the input string—click Refresh to set it back to its original value.

Not all methods require three parameters, and you'll see the parameter text boxes appear as necessary. In addition, some methods provide multiple overloaded versions which may allow you to specify fewer parameters. For example, the IndexOf method (on the "Methods that Return Information" page) can accept 1, 2, or 3 parameters. You can try the method various ways, and the code should handle each test appropriately.

If you pass incorrect parameters (if you enter the wrong data type, for example), the form will display the error message instead of results.

In addition to the String class, the sample form also introduces the StringBuilder and StringWriter classes. For multiple operations on the same string, you may find the StringBuilder class to be more efficient than using a String object. Because the String object is immutable-that is, once you've set its value, you can't change it. The only alternative is to return a new string if you attempt to insert, delete, or modify any portion of a string. The StringBuilder class provides an internal buffer whose size is managed by the class itself. You simply call methods of the StringBuilder object you create, and the .NET Framework manages the buffer containing your text. When you're done with the manipulations, you can use the ToString method of the object to retrieve the text. Watch out, however—not all String class methods are available when you're using the StringBuilder, and in cases where you need those methods (IndexOf is one example of such a method), you'll need to retrieve the contents as a string, do your work on that string, and then continue with the StringBuilder. The StringBuilder class does provide all the methods you'll need in order to manipulate text, however. Click the StringBuilder button on the Other Classes tab to test out this class.

The StringWriter class is useful when you need to append text to an output string, optionally including carriage return/line feed character. (This class uses a StringBuilder, under the covers.) Although you can do this sort of work using a String object, your code can be messy and somewhat inefficient. The StringWriter class provides an internal buffer to which you can write text as if you were writing to a file. You call the Write and/or WriteLine methods to append text to the buffer, and you can retrieve the text using the ToString method. Click the StringWriter button on the Other Classes tab to test this class.

Requirements

Requires the Trial or Release version of Visual Studio .NET Professional (or greater).

Running the Sample

The demo code for this example is somewhat complex—this complexity allows you to interact with the String class methods at runtime. Note the following features that may be of interest, as you peruse the sample code:

pnlDemo.Parent = tabStringDemo.SelectedTab

Last Update: 7 July 2002