String Methods

Objectives


Discussion

Traditional String Functions  and String Methods (VB.NET)

VB.NET method Traditional VB function Description
IndexOf Instr() Determines if one string in another
SubString Mid(), Left(), Right() Extracts a sub-string from a string
Length Len() Returns length of string
toUpper UCase() Converts a string to upper case

Back to top


Demonstration

In this demo we will use some of the string methods.  Make sure that you check out the methods that are available. 

1.  Open your Unit 6 project and add a new form.

2.  Add a button to the form and open the click event for the button.  Type the following to declare a string:  "Dim sX as String".  Then type "sX." so that the list of available methods appears.  Scroll down through the list and read the descriptions of each.  There are so many methods and we will discuss only a few.

3.  Now delete what you just typed and copy and paste the following in the click event for the button.  (Depending on the text size in your browser,  some of the lines may wrap around to the next line.):

MessageBox.Show("Chr 33 is: " & Chr(33))
Dim sX As String = "The quick brown fox jumps over the lazy dog"
MessageBox.Show("The string has " & sX.Length & " characters")
MessageBox.Show("The first e is at position " & sX.IndexOf("e"))
MessageBox.Show("The next e starting from the 5th position is at: " & sX.IndexOf("e", 5))
MessageBox.Show("The 5 character substring starting at position 4 is: " & sX.Substring(4, 5))
MessageBox.Show("Converting the string to upper: " & sX.ToUpper)
MessageBox.Show("Removing 11 characters starting at position 4 yields: " & sX.Remove(4, 11))
MessageBox.Show("Replacing each e with E yields: " & sX.Replace("e", "E"))

4.  Run the program and check out each message box and the line of code that generated it. Your goal is to understand what the methods are doing.

5.  We will now demonstrate what you can do with string methods.  We will write a function that gets the first word in a string.  We will do this by determining where the space (" ") is using the IndexOf method.  If there is no space in the string then the method returns -1.  Once the location of the space is determined, the substring is pulled out.  The substring starts with the first character (character 0) and has a length equal to the location of the space.

Private Function GetFirstWord(ByVal sSentence As String) As String
   Dim i As Int16
   i = sSentence.IndexOf(" ")
   If i = -1 Then Return sSentence 
   sSentence = sSentence.Substring(0, i)
   Return sSentence
End Function

6.  Add a button and a textbox (txtString).  In the click event for the button, call the function by passing in txtString.text and return the result to a messagebox.

MessageBox.Show(GetFirstWord(Me.txtString.Text))

7.  Run your program and experiment.

Back to top
Exercises

1.  Write a sub that accepts a string and then displays each character in the string individually in messageboxes. For example if you pass the word "the" into the sub then three messageboxes should appear one after another.  The first messagebox should have "t" in it, the second an "h", and the third an "e".  Test your sub in a program.

2.  Write a function that accepts a string and returns the string with all numbers eliminated from it.  Then create a simple program to test your function.

 3.  Write a function that accepts a string and returns the string with the first letter of it uppercase and the remaining letters lowercase.  Test your function in a program.

Back to top
Links & Help
Back to top