VB2022 VB2019 VB6 VB Sample Code About Us

Lesson 12 String Manipulation


In Visual Basic 2019, a string is a single unit of data that made up of a series of characters that include letters, digits, alphanumeric symbols(@,#,$,%,^,&,*, etc) and more. It is treated as the String data type. It is non-numeric in nature, though it might consist of numbers. Everyday life examples of strings are names, addresses, gender, cities, book titles, phone numbers, email addresses and more. In Visual Basic 2019, you can manipulate strings by writing code to process characters like sentences, words, text , alphanumeric characters and more.

12.1 String Manipulation Using + and & signs.

You can manipulate strings using the & sign and the + sign, both perform the string concatenation which means combining two or more smaller strings into larger strings. For example, we can join "Visual","Basic" and "2019" into "Visual Basic 2017" using "Visual"&"Basic" or "Visual "+"Basic", as shown in the Examples below:

Example 12.1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim str1 = "Visual ", str2 = "Basic ", str3 = "2019", str As String
str = str1 + str2 + str3
MsgBox(str)
End Sub

The line str = str1 + str2 + str3 can be replaced by str = str1 & str2 & str3 and produces the same output. However, if one of the variables is declared as numeric data type, you cannot use the + sign, you can only use the & sign.

The output is shown in Figure 12.1

Figure 12.1

Example 12.2

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim str1 = "Visual ", str2 = "Basic ", str3 = "2019", str As String
Dim str4 As Integer
str4 = 100
str = str1 + str2 + str3 + str4
MsgBox(str)

End Sub

This code will produce an error because of data mismatch, as shown in Figure 12.2.

Figure 12.2

However, using & instead of + will fix the error as the integer will be treated as a string. The output is as follows:

Figure 12.3

12.2 String Manipulation Using VB2019 Built-in Functions

A function is similar to a normal procedure but the main purpose of the function is to accept a certain input and return a value which is passed on to the main program to finish the execution. There are numerous string manipulation functions that are built into Visual Basic 2019.

12.2 (a) The Len Function

The Len function returns an integer value which is the length of a phrase or a sentence, including the empty spaces. The syntax is

Len (“Phrase”)

For example,

Len (Visual Basic 2019) = 17 and Len ("welcome to vb 2019 tutorial") = 27

Example 12.3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyText as String
MyText="Visual Basic 2019"
MsgBox("The Length of" & MyText & "is" & Len(MyText))
End Sub

The output:

 Figure 12.4

12.2(b) The Right Function

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

Microsoft.VisualBasic.Right(“Phrase”,n)

Example 12.4

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyText As String
MyText="Visual Basic"
MsgBox("The four rightmost characters of " & myText & " is " & Microsoft.VisualBasic.Right(MyText, 4))
End Sub

The above program returns four rightmost characters of the phrase entered into the textbox.

The Output:

vb2013_figure12.5
Figure 12.5
 

12.2(c)The Left Function

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

Microsoft.VisualBasic.Left(“Phrase”,n)

Where n is the starting position from the left of the phase where the portion of the phrase is will be extracted.

Example 12.5

Dim MyText As String
MyText="Visual Basic"
MsgBox("The four rightmost characters of " & myText & " is " & Microsoft.VisualBasic.Left(MyText, 4))

The output:

vb2013_figure12.5
Figure 12.6

12.2 (d) The Mid Function

The Mid function is used to extract a portion of text from a given phrase. The syntax of the Mid Function is

Mid(phrase, position,n)

where phrase is the string from which a part of the text is to be extracted, position is the starting position of the phrase from which the extraction process begins. Lastly, n is the number of characters to extract.

Example 12.6

In this example, we insert two Labels, one to display the phrase entered and the other one to display the extracted phrase. Besides that, we add two text boxes, one of them is for the user to enter the position to start extraction while the other one is for the user to enter the number of characters to be extracted. In addition, insert two buttons, one is for the user to click and enter the phrase in the InputBox while another one for displaying the extracted phrase. The code is as follows:

Public Class Form1
    Dim myPhrase As String
    Dim pos As String
    Dim num As String
    Private Sub BtnEnter_Click(sender As Object, e As EventArgs) Handles BtnEnter.Click
        myPhrase = InputBox("Enter your phrase")
        LblPhrase.Text = myPhrase
    End Sub

    Private Sub BtnExtract_Click(sender As Object, e As EventArgs) Handles BtnExtract.Click
        pos = TxtPos.Text
        num = TxtNum.Text
        LblExtract.Text = Mid(myPhrase, pos, num)
    End Sub
End Class

* In this example, when the user clicks the button, an input box will pop up prompting the user to enter a phrase. After a phrase is entered and the 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.

Figure 12.7

12.2(e) The Trim Function

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

Trim(“Phrase”)

For example,

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

12.2(f) The Ltrim Function

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

Ltrim("Phrase")

For example,

Ltrim ("     Visual Basic 2019″)= Visual basic 2019

12.2(g)The Rtrim Function

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

Rtrim("Phrase")

For example,

Rtrim ("Visual Basic 2019    ") = Visual Basic 2019

12.2(h) The InStr function

The 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)

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

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

*The function returns a numeric value.

You 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", "Basic")
End Sub

12.2(i) The Ucase and the Lcase Functions

The 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. The syntaxes  are

Microsoft.VisualBasic.UCase(Phrase)

Microsoft.VisualBasic.LCase(Phrase)

For example,

Microsoft.VisualBasic.Ucase("Visual Basic") =VISUAL BASIC

Microsoft.VisualBasic.Lcase("Visual Basic") =visual basic

12.2(j)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)

and the syntax of the Asc function is

Asc(Character)

The following are some examples:

Chr(65)=A, Chr(122)=z, Chr(37)=% ,

Asc("B")=66, Asc("&")=38


❮ Previous lesson Next lesson ❯


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