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.
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:
12.1.2 Handling Data Type Mismatch
When concatenating strings with non-string data types, you should use the & operator to avoid errors.
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:
12.2 Built-in String Functions
Visual Basic 2019 includes numerous built-in functions for string manipulation. Here are the most commonly used ones:
| 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.
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:
12.2.2 Left and Right Functions
These functions extract portions from the left or right side of a string.
Left Function
Extracts the left portion of a string
Example: Microsoft.VisualBasic.Left("Visual Basic", 6) returns "Visual"
Right Function
Extracts the right portion of a string
Example: Microsoft.VisualBasic.Right("Visual Basic", 5) returns "Basic"
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:
12.2.3 The Mid Function
The Mid function extracts a substring from the middle of a string.
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
12.2.4 Trimming Functions
These functions remove unwanted spaces from strings.
Trim Function
Removes spaces from both ends
Example: Trim(" Hello ") returns "Hello"
LTrim Function
Removes spaces from left end
Example: LTrim(" Hello") returns "Hello"
RTrim Function
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.
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:
12.2.6 Case Conversion Functions
These functions convert strings to uppercase or lowercase.
UCase Function
Converts to uppercase
Example: Microsoft.VisualBasic.UCase("Visual") returns "VISUAL"
LCase Function
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
Returns character from ASCII code
Example: Chr(65) returns "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
Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.
Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.