String Methods Sample 

Download 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.

Security noteSecurity Note

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To open the sample file in Solution Explorer

  1. Click Download Sample.

    The File Download message box appears.

  2. Click Open, and on the left column of the zip folder window, click Extract all files.

    The Extraction Wizard opens.

  3. Click Next. You can change the directory that the files will be extracted to, and then click Next again.

  4. Make sure that the Show extracted files check box is selected, and click Finish.

  5. Double-click the sample's .sln file.

    The sample solution is displayed in Solution Explorer. You might get a security warning that says the solution location is not trusted. Click OK to continue.

To run this sample

  • Press F5.

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 allows 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

System.String.Insert(System.Int32,System.String)

System.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.

System.String.IndexOf

System.String.StartsWith

System.String.EndsWith

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

System.String.Format

System.String.Join

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

System.Text.StringBuilder.ToString

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

System.IO.StringWriter.Write

System.IO.TextWriter.WriteLine

System.IO.StringWriter.ToString

The StringWriter class is useful when you need 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 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 look like buttons, but stay selected once 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 containing all the "common" controls to be the selected page, like this:

pnlDemo.Parent = tabStringDemo.SelectedTab 

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

See Also

Community Additions

ADD
Show: