Lesson 12: String Manipulation in VB2019

Master text processing techniques to build powerful applications

Key Takeaway

Visual Basic 2019 provides powerful string manipulation functions that enable you to process, analyze, and transform text data efficiently in your applications.

String manipulation is fundamental to programming. In Visual Basic 2019, a string is a single unit of data made up of a series of characters that include letters, digits, alphanumeric symbols (@,#,$,%,^,&*, etc) and more. Everyday life examples of strings are names, addresses, gender, cities, book titles, phone numbers, and email addresses.

12.1 String Concatenation

You can manipulate strings using the & sign and the + sign. Both perform string concatenation which means combining two or more smaller strings into larger strings.

Concatenation Operators

The + operator can only be used with strings, while the & operator can concatenate strings with other data types by converting them to strings first.

12.1.1 Basic String Concatenation

This example demonstrates how to combine strings using the + and & operators.

Form1.vb
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

Output:

> Visual Basic 2019
String concatenation output
Figure 12.1: String concatenation output

12.1.2 Handling Data Type Mismatch

When concatenating strings with non-string data types, you should use the & operator to avoid errors.

Form1.vb
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
    ' Using + will cause an error
    ' str = str1 + str2 + str3 + str4
    
    ' Using & will work correctly
    str = str1 & str2 & str3 & str4
    MsgBox(str)
End Sub

Output:

> Visual Basic 2019100
Data type mismatch handling
Figure 12.2: Correct handling of mixed data types

12.2 Built-in String Functions

Visual Basic 2019 includes numerous built-in functions for string manipulation. Here are the most commonly used ones:

Table 12.1: String Functions in VB2019
Function Description Example Result
Len Returns the length of a string Len("Visual Basic") 12
Left Extracts left portion of a string Microsoft.VisualBasic.Left("Visual Basic", 6) "Visual"
Right Extracts right portion of a string Microsoft.VisualBasic.Right("Visual Basic", 5) "Basic"
Mid Extracts substring from middle Mid("Visual Basic", 8, 5) "Basic"
Trim Removes spaces from both ends Trim(" Hello ") "Hello"
LTrim Removes spaces from left end LTrim(" Hello") "Hello"
RTrim Removes spaces from right end RTrim("Hello ") "Hello"
InStr Finds position of substring InStr(1, "Visual Basic", "Basic") 8
UCase Converts to uppercase Microsoft.VisualBasic.UCase("Visual") "VISUAL"
LCase Converts to lowercase Microsoft.VisualBasic.LCase("Basic") "basic"
Chr Returns character from ASCII code Chr(65) "A"
Asc Returns ASCII code of character Asc("A") 65

12.2.1 The Len Function

The Len function returns the length of a string, including spaces.

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

Output:

> The Length of Visual Basic 2019 is 17
Len function output
Figure 12.3: Len function output

12.2.2 Left and Right Functions

These functions extract portions from the left or right side of a string.

Left Function

L

Extracts the left portion of a string

Example: Microsoft.VisualBasic.Left("Visual Basic", 6) returns "Visual"

Right Function

R

Extracts the right portion of a string

Example: Microsoft.VisualBasic.Right("Visual Basic", 5) returns "Basic"

Form1.vb
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim MyText As String
    MyText = "Visual Basic"
    Dim leftPart = Microsoft.VisualBasic.Left(MyText, 6)
    Dim rightPart = Microsoft.VisualBasic.Right(MyText, 5)
    
    MsgBox("Left: " & leftPart & " | Right: " & rightPart)
End Sub

Output:

> Left: Visual | Right: Basic

12.2.3 The Mid Function

The Mid function extracts a substring from the middle of a string.

Form1.vb
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
Mid function example
Figure 12.4: Mid function example interface

12.2.4 Trimming Functions

These functions remove unwanted spaces from strings.

Trim Function

T

Removes spaces from both ends

Example: Trim(" Hello ") returns "Hello"

LTrim Function

L

Removes spaces from left end

Example: LTrim(" Hello") returns "Hello"

RTrim Function

R

Removes spaces from right end

Example: RTrim("Hello ") returns "Hello"

12.2.5 The InStr Function

Finds the position of a substring within another string.

Form1.vb
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim position = InStr(1, "Visual Basic", "Basic")
    Label1.Text = position
End Sub

Output:

> Position: 8

12.2.6 Case Conversion Functions

These functions convert strings to uppercase or lowercase.

UCase Function

U

Converts to uppercase

Example: Microsoft.VisualBasic.UCase("Visual") returns "VISUAL"

LCase Function

L

Converts to lowercase

Example: Microsoft.VisualBasic.LCase("Basic") returns "basic"

12.2.7 Character and ASCII Functions

These functions work with character codes and ASCII values.

Chr Function

C

Returns character from ASCII code

Example: Chr(65) returns "A"

A

Asc Function

Returns ASCII code of character

Example: Asc("A") returns 65

Lesson Summary

In this lesson, you've learned essential string manipulation techniques in Visual Basic 2019:

String Concatenation

Mastered the use of + and & operators to combine strings

Built-in Functions

Learned essential functions like Len, Left, Right, Mid, Trim, InStr, UCase, and LCase

Character Handling

Used Chr and Asc functions to work with character codes

Practical Applications

Implemented real-world examples demonstrating each function's usage

String manipulation is fundamental to building functional applications. In the next lesson, we'll explore conditional logic with If..Then..Else statements.

Next Lesson

Ready to learn about conditional logic? Continue to Lesson 13: If..Then..Else.

Related Resources

VB6 Tutorial

Mastering VB6 Programming

Explore Tutorials

Visual Basic Examples

Practical VB code samples for real-world applications

View Examples

Excel VBA Tutorial

Learn how to automate Excel by creating VBA macros

Learn More

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon