Function methods of the String class

As we have seen, methods are the verbs of programming, they cause action. The AppendText method, adds text to a Text property, the Clear method, resets a Text property, the Close method terminates an object, etc. These are examples of subprogram methods. They do some computation and their effect is to change a global variable, a property value or an object.

Function methods (we will call them "functions" for short) are similar in that they do some computation, but they communicate the result to the rest of the program by returning a value.

This note introduces some of the function methods of the String class.

The following is a listing of a String function program that uses three functions of the String class (ToLower, ToUpper, and Substring) and one property (Length).

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   Dim strA As String = "This is a string."
   Label1.Text = strA
   Label2.Text = CStr(strA.Length)
   Label3.Text = strA.ToLower
   Label4.Text = strA.ToUpper
   Label5.Text = strA.Substring(3, 8)
   Label6.Text = strA.Substring(0, 3)
   Label7.Text = strA.Substring(10)
   Label8.Text = strA.Substring(strA.Length - 7, 7)
End Sub

When you click the Evaluate the Functions button, each function is evaluated and the value it returns is displayed.

The Length property contains the length of the string. ToUpper and ToLower convert the string to all upper case or lower case letters, and the Substring method returns a designated portion of the string.

These are summarized in the following table:

Function/
Property
Returns Syntax Data type Number of Arguments
Length The length of the string <string name>.Length integer n.a.
ToLower The string converted to lower case <string name>.ToLower() string 0
ToUpper The string converted to upper case <string name>.ToUpper() string 0
Substring a subset of the characters in the string <string name>.Substring(<integer expression>[, <integer expression>]) string 1 or 2

Like an expression, a function returns a single value when evaluated. Functions also have data types determined by the type of the value they return.

As you see, we are using the familiar syntax:

<object>.<method>
The objects in this are the strings and the functions are the methods.

The Substring function requires one argument, and it can take an optional second argument. If only one argument is present, SubString returns the right hand end of the string, starting with the specified character. If two arguments are given, SubString returns a string that starts with the character specified in the first argument and has the length specified in the second argument.

The character positions in the string are numbered beginning with 0. For example, the string "Larry" has a length of five characters, numbered from 0 through 4.

Functions like Substring that can take a varying number of arguments or in which the arguments may have optional data types, are said to be overloaded.

Note that some functions, like ToUpper and ToLower, do not require any arguments, and they are called with empty parenthesis ().

These are just a few of the functions of the String class, and VB comes with many other classes with function methods. They are described in the online Help.

Functions are a way to capture a certain computation for convenient re-use throughout a program, and programmers can extend VB by writing their own functions. You will learn to do that in a subsequent note.

Finally, note that the string property Length could have been implemented as a function had the designers of VB chosen to do so. From a programmer's point of view, it would not have mattered.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.