String Methods Sample

To get samples and instructions for installing them, see the following:

  • Click Samples on the Visual Studio Help menu.

    For more information, see Locating Sample Files.

  • The most recent versions and the complete list of samples are available on the Visual Studio 2008 Samples Web site.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied to a folder under \Program Files\Visual Studio 9\Samples\. For Visual Studio Express Editions, all samples are located on the Internet.

To view the sample documentation

  1. In Solution Explorer, double-click the Documentation folder.

  2. If you are using Visual Basic Express Edition, right-click ReadMe.htm in the Documentation folder. Select View in Browser.

  3. If you are using another version of Visual Basic, double-click ReadMe.htm in the Documentation folder.

Demonstrates

The main form contains a TabControl with three tab pages that demonstrate String member methods, String shared methods, and StringWriter methods. Each tab page enables the user to enter string values and then execute String methods by clicking buttons. The underlying design contains a Method class and a Parameter class. Each instance of the Method class represents a different String method. This design makes it easy to pass the values entered on the form to the appropriate String method.

Method

Description

String..::.Insert

String..::.Remove

These methods create and return new String objects. Many of these methods are overloaded and take one, two, or three parameters. The code may ignore some of the input fields in the form.

String..::.IndexOf

String..::.StartsWith

String..::.EndsWith

These methods return information about an existing string, but do not create or modify String objects.

String..::.Format

String..::.Join

These methods often need two Strings to complete a task, or create new strings, and are therefore implemented as Shared methods.

StringBuilder..::.ToString

The StringBuilder class enables you to manipulate the characters in the string. The ToString method retrieves the text that is contained by the StringBuilder object.

StringWriter..::.Write

TextWriter..::.WriteLine

StringWriter..::.ToString

The StringWriter class is useful when you have to append text to an output string. The StringWriter class provides an internal buffer to which you can write text as if you were writing to a file. The Write and WriteLine methods append text to the buffer. The ToString method retrieves the text that is contained by the StringWriter object.

The buttons listing String class methods are actually RadioButton controls. The button appearance is obtained by setting the Appearance property to Button. The controls resemble buttons, but stay selected when clicked.

The buttons used to select the String class methods all call into the same event handler, HandleCheckedChanged. This procedure uses many Handles clauses. Inside the procedure, an If...Then statement uses the sender parameter to determine which button was selected and acts appropriately.

There is no way to float controls on top of a tab control, so that a single instance of a group of controls appears on each page. To provide that feature in this sample, selecting a page on the tab control sets the Parent property of a Panel control that contains all the "common" controls to be the selected page, like this:

pnlDemo.Parent = tabStringDemo.SelectedTab 

To trigger a breakpoint so that you can walk through the StringBuilder and StringWriter code, the sample uses the Debugger..::.Break method. This method is called if the CheckBox control labeled Step through code is selected.