Lesson 12

String Manipulation

In Visual Basic 2015, a string is a unit of data made up of characters such as letters, digits, spaces, punctuation marks, and symbols. In real applications, strings are used for names, addresses, titles, phone numbers, email addresses, messages, and more. This lesson explains how to join strings together and how to manipulate them using useful built-in functions.

Lesson focus:

String handling is essential in programming because many real-world applications work with text, such as word processing, forms, contact lists, search tools, and data entry systems.

Lesson Overview

Lesson12
TopicString Manipulation
Main FocusConcatenation and Functions
Key SkillsExtract, Trim, Search, Convert
Next StepIf..Then..Else
12.1 Concatenation
12.2 Built-in Functions
12.3 Text Extraction
12.4 Character Conversion

What Is a String?

A string is a single unit of data made up of a sequence of characters. These characters may include letters, digits, spaces, and special symbols such as @, #, $, %, ^, and &.

Even if a string contains digits, it is still treated as text unless it is explicitly converted into a numeric value. That is why a phone number is stored as a string rather than a number.

In Visual Basic 2015, string manipulation refers to processing or editing text. This includes combining text, extracting part of a text, trimming spaces, changing letter case, searching for a word, and more.

Combining Strings with + and &

In Visual Basic 2015, both the + sign and the & sign can be used to join strings together. This operation is called string concatenation.

For example, you can join the strings "Visual ", "Basic ", and "2015" to produce "Visual Basic 2015".

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim str1 = "Visual ", str2 = "Basic ", str3 = "2015", str As String
    str = str1 + str2 + str3
    MsgBox(str)

End Sub

The statement above can also be written as:

str = str1 & str2 & str3

Both versions produce the same result when all values are strings.

VB2015 Figure 12.1 Concatenation output

Figure 12.1: Output of string concatenation

Why + May Cause an Error

If one of the variables is numeric, using the + sign may cause a type mismatch error. Consider the following example:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim str1 = "Visual ", str2 = "Basic ", str3 = "2015", str As String
    Dim str4 As Integer
    str4 = 100
    str = str1 + str2 + str3 + str4
    MsgBox(str)

End Sub

This produces an error because Visual Basic tries to treat the expression inconsistently when text and numeric values are combined with the + operator.

VB2015 Figure 12.2 Type mismatch error

Figure 12.2: Type mismatch error

To avoid this problem, use the & sign instead. With &, the integer is treated as text automatically:

str = str1 & str2 & str3 & str4

This works correctly and produces the combined output.

VB2015 Figure 12.3 Correct concatenation output using ampersand

Figure 12.3: Correct output using the & sign

Useful String Functions in Visual Basic 2015

Visual Basic 2015 provides many useful built-in string functions. These functions accept input and return a value that your program can use immediately.

Some of the most common functions are:

  • Len — returns the length of a string
  • Right — extracts characters from the right side
  • Left — extracts characters from the left side
  • Mid — extracts characters from the middle
  • Trim, LTrim, RTrim — remove spaces
  • InStr — finds the position of embedded text
  • UCase and LCase — convert case
  • Chr and Asc — convert between characters and ASCII codes

The Len Function

The Len function returns the number of characters in a string, including spaces.

Len("Visual Basic 2015") = 17
Len("welcome to VB 2015 tutorial") = 27

Example:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim MyText As String
    MyText = "Visual Basic 2015"
    MsgBox(Len(MyText))

End Sub
VB2015 Figure 12.4 Output of Len function

Figure 12.4: Output of the Len function

The Right Function

The Right function extracts a specified number of characters from the right side of a string.

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

Example:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim MyText As String
    MyText = "Visual Basic"
    MsgBox(Microsoft.VisualBasic.Right(MyText, 4))
End Sub

This example returns the four rightmost characters of the phrase.

VB2015 Figure 12.5 Output of Right function

Figure 12.5: Output of the Right function

The Left Function

The Left function extracts a specified number of characters from the left side of a string.

Microsoft.VisualBasic.Left("Phrase", n)

For example:

Microsoft.VisualBasic.Left("Visual Basic", 4) = "Visu"

The Mid Function

The Mid function retrieves a portion of text from the middle of a phrase.

Mid(phrase, position, n)
  • phrase is the original string.
  • position is the starting position.
  • n is the number of characters to extract.

Example:

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

In this example, the extracted text begins at position 2 and contains 6 characters.

VB2015 Figure 12.6 Output of Mid function example

Figure 12.6: Output of the Mid function example

The Trim, LTrim, and RTrim Functions

These functions remove unnecessary spaces from a string:

  • Trim removes spaces on both sides
  • LTrim removes spaces on the left side
  • RTrim removes spaces on the right side
Trim(" Visual Basic ") = "Visual Basic"
LTrim("     Visual Basic 2015") = "Visual Basic 2015"
RTrim("Visual Basic 2015     ") = "Visual Basic 2015"

Example:

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

The InStr Function

The InStr function searches for an embedded phrase within a larger phrase and returns the position where it begins.

InStr(n, originalPhrase, embeddedPhrase)

Example:

InStr(1, "Visual Basic 2015", "Basic") = 8

Program example:

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

The function returns a numeric value that shows the starting position of the embedded word.

The UCase and LCase Functions

UCase converts all characters to uppercase, while LCase converts all characters to lowercase.

Microsoft.VisualBasic.UCase("Visual Basic") = "VISUAL BASIC"
Microsoft.VisualBasic.LCase("Visual Basic") = "visual basic"

The Chr and Asc Functions

The Chr function returns the character that matches an ASCII code. The Asc function returns the ASCII code for a given character.

Chr(charcode)
Asc(character)

Examples:

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

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

ASCII stands for American Standard Code for Information Interchange. It is a standard system for representing characters with numbers.

Why String Manipulation Is Important

Core takeaway:

String manipulation is one of the most practical parts of programming because real applications often work with names, addresses, messages, search terms, user input, and formatted output. Once you master these functions, you can build much more powerful text-processing programs.

Build on This Foundation

Continue to VB2026

After learning the basics of string manipulation in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.

Explore VB2026 →

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Exercise Questions

  1. What is the difference between using + and & for concatenation in Visual Basic 2015?
  2. Write a short example using Mid to extract 5 characters from a phrase.
  3. What do the Chr and Asc functions do?

Go to Lesson 13

In the next lesson, you will learn how to use If..Then..Else statements for decision making in Visual Basic 2015.