String Methods Sample

This sample demonstrates several methods for manipulating strings. The purpose of the sample is not to show how to call the functions, but to demonstrate what the functions can do for you. The main form contains one TabControl with several tab pages. Each tab page is devoted to a String programming feature.

To get samples and instructions for installing them

  • Do one or more of the following:

    • On the Help menu, click Samples.

      The Readme displays information about samples.

    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.

    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.

For more information, see Visual Studio Samples.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To view the sample documentation

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

  2. If you are using Visual Basic Express, 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.

See Also

Reference

String

StringBuilder

StringWriter

Debugger.Break

Handles

Other Resources

TabControl Control (Windows Forms)