>
VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Code 中文VB About Us

Lesson 13 : String functions


We have introduced the basic concept of functions in the previous lesson. In this lesson, we will examine the built-in functions in VB2010. As a matter of facts, we have introduced three built-in functions in Lesson 8, they are the Len function, the Left function, and the Right Function. In this lesson, you will learn additional built-in functions.

13.1 The Mid Function

The Mid function is used to retrieve a part of the text from a given phrase. The syntax is

Mid(phrase, position,n)
phrase is the string from which a part of the text is to be retrieved. position is the starting position of the phrase from which the retrieving process begins. n is the number of characters to retrieve.

Example 13.1:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myPhrase As String
 myPhrase = Microsoft.VisualBasic.InputBox("Enter your phrase")
 Label1.Text = Mid(myPhrase, 2, 6)
End Sub

* When the user clicks the command button, an input box will pop up asking the user to enter a phrase. After a phrase is entered and OK button is pressed, the label will show the extracted text starting from position 2 of the phrase and the number of characters extracted is 6, as shown in the figures below:

Visual Basic 2010

Figure 13.1

Visual Basic 2010

Figure 13.2

13.2 The Right Function

The Right function extracts the right portion of a phrase. The syntax is

Microsoft.Visualbasic.Right ("Phrase", n)

Where n is the starting position from the right of the phase where the portion of the phrase is going to be extracted. For example:

Microsoft.Visualbasic.Right ("Visual Basic", 4) = asic

Example 13.2

The following code extracts the right portion any phrase entered by the user.

Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myword As String
 myword = TextBox1.Text
 Label1.Text = Microsoft.VisualBasic.Right (myword, 4)
End Sub

13.3 The Left Function

The Left function extracts the left portion of a phrase. The syntax is

Microsoft.Visualbasic.Right ("Phrase", n)

TWhere n is the starting position from the left of the phase where the portion of the phrase is going to be extracted. For example:

Microsoft.Visualbasic.Left("Visual Basic", 4) = asic

Example 13.3

TThe following code extracts the left portion any phrase entered by the user.

Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 Dim myword As String
 myword = TextBox1.Text
 Label1.Text = Microsoft.VisualBasic.Left (myword, 4)
End Sub

13.4 The Trim Function

TThe Trim function trims the empty spaces on both sides of the phrase. The format is

Trim("Phrase")

For example,

Trim ("   Visual Basic 2010     ") = Visual basic 2010

Example 13.4

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myPhrase As String
 myPhrase = Microsoft.VisualBasic.InputBox("Enter your phrase")
 Label1.Text = Trim(myPhrase)
End Sub

13.5 The Ltrim Function

TThe Ltrim function trims the empty spaces of the left portion of the phrase. The syntax is

Ltrim("Phrase")

TFor example,

Ltrim ("    Visual Basic 2010 ")= Visual basic 2010

13.6 The Rtrim Function

TThe Rtrim function trims the empty spaces of the right portion of the phrase. The syntax is

Rtrim("Phrase")

TFor example,

Rtrim ("Visual Basic      ") = Visual Basic

13.7 The InStr function

TThe InStr function looks for a phrase that is embedded within the original phrase and returns the starting position of the embedded phrase. The syntax is

Instr (n, original phase, embedded phrase)

TWhere n is the position where the Instr function will begin to look for the embedded phrase. For example

Instr(1, "Visual Basic 2010 ","Basic")=8

T*The function returns a numeric value.

TYou can write a program code as shown below:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 Label1.Text = InStr(1, "Visual Basic 2010", "Basic")
End Sub

13.8 The Ucase and the Lcase Functions

TThe Ucase function converts all the characters of a string to capital letters. On the other hand, the Lcase function converts all the characters of a string to small letters.

TThe syntax is

Microsoft.VisualBasic.UCase(Phrase)
Microsoft.VisualBasic.LCase(Phrase)

TFor example,

Microsoft.VisualBasic.Ucase("Visual Basic 2010") =VISUAL BASIC 2010
Microsoft.VisualBasic.Lcase("Visual Basic 2010") =visual basic 2010

13.9 The Chr and the Asc functions

The Chr function returns the string that corresponds to an ASCII code while the Asc function converts an ASCII character or symbol to the corresponding ASCII code. ASCII stands for "American Standard Code for Information Interchange". Altogether there are 255 ASCII codes and as many ASCII characters. Some of the characters may not be displayed as they may represent some actions such as the pressing of a key or produce a beep sound. The syntax of the Chr function is

Chr(charcode)

Tand the syntax of the Asc function is

Asc(Character)

TThe following are some examples:

Chr(65)=A, Chr(122)=z, Chr(37)=% 
Asc("B")=66, Asc("&")=38

13.10 MsgBox ( ) Function

TThe objective of MsgBox is to produce a pop-up message box and prompts the user to click on a command button before he or she can continues. This syntax is as follows:

yourMsg=MsgBox(Prompt, Style Value, Title)

TThe first argument, Prompt, will display the message in the message box. The Style Value will determine what type of command buttons appear on the message box, please refer to Table 12.1 for types of command button displayed. The Title argument will display the title of the message board.

Table 13.1

Style Value Named Constant Buttons Displayed
0 vbOkOnly Ok button
1 vbOkCancel Ok and Cancel buttons
2 vbAbortRetryIgnore Abort, Retry and Ignore buttons.
3 vbYesNoCancel Yes, No and Cancel buttons
4 vbYesNo Yes and No buttons
5 vbRetryCancel Retry and Cancel buttons

TWe can use named constants in place of integers for the second argument to make the programs more readable. In fact, Visual Basic 2010 will automatically show up a list of named constants where you can select one of them.

Examples:

 yourMsg=MsgBox( "Click OK to Proceed", 1, "Startup Menu")
and
yourMsg=Msg("Click OK to Proceed". vbOkCancel,"Startup Menu")

TyourMsg is a variable that holds values that are returned by the MsgBox ( ) function. The values are determined by the type of buttons being clicked by the users. It has to be declared as Integer data type in the procedure or in the general declaration section. Table 13.2 shows the values, the corresponding named constant and buttons.

Table 13.2

Value Named Constant Button Clicked
1 vbOk Ok button
2 vbCancel> Cancel button
3 vbAbort Abort button
4 vbRetry Retry button
5 vbIgnore Ignore button
6 vbYes Yes button
7 vbNo No button

Example 13.5

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim testmsg As Integer
 testmsg = MsgBox("Click to test", 1, "Test message")
 If testmsg = 1 Then
  MessageBox.Show("You have clicked the OK button")
 Else
  MessageBox.Show("You have clicked the Cancel button")
 End If
End Sub

TTo make the message box looks more sophisticated, you can add an icon beside the message. There are four types of icons available in Visual Basic 2010 as shown in Table 13.3

Table 13.3

Value Named Constant Icon
16 vbCritical
3 vbQuestion
48 vbExclamation
64 vbInformation

Example 13.6

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim testMsg As Integer
 testMsg = MsgBox("Click to Test", vbYesNoCancel + vbExclamation, "Test Message")
 If testMsg = 6 Then
  MessageBox.Show("You have clicked the yes button")
 ElseIf testMsg = 7 Then
  MessageBox.Show("You have clicked the NO button")
 Else
  MessageBox.Show("You have clicked the Cancel button")
 End If
End Sub

TThe first argument, Prompt, will display the message

13.11 The InputBox( ) Function

TAn InputBox( ) function will display a message box where the user can enter a value or a message in the form of text.

The syntax to call up an Input Box is

Microsoft.VisualBasic.InputBox(Prompt, Title, default_text, x-position, y-position)

Example 13.7

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim userMsg As String
 userMsg = Microsoft.VisualBasic.InputBox("What is your message?", "Message Entry Form", "Enter your messge here", 500, 700)
 If userMsg <> "" Then
 MessageBox.Show(userMsg)
 Else
  MessageBox.Show("No Message")
 End If
End Sub

The input box will appear as shown in the figure below when you press the command button


❮ Previous Lesson Next Lesson ❯


Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy