Lesson 13: Mastering String Functions in VB6

Learn essential string manipulation functions for text processing and data handling

Key Takeaway

String functions in VB6 provide powerful tools for text manipulation, enabling you to extract, transform, and analyze textual data efficiently.

Welcome to Lesson 13 of our Visual Basic 6 Tutorial! In this lesson, you'll master VB6's essential string manipulation functions. These functions are crucial for creating applications that process and analyze textual data effectively.

13.1 The Len Function

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

Syntax

Len(string)
Len("VisualBasic") → 11
Len("welcome to VB tutorial") → 22

13.2 The Right Function

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

Syntax

Right(string, length)
Right("Visual Basic", 4) → "asic"

13.3 The Left Function

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

Syntax

Left(string, length)
Left("Visual Basic", 4) → "Visu"

13.4 The LTrim, RTrim and Trim Functions

These functions remove whitespace from strings:

1 LTrim

Removes leading whitespace from a string.

LTrim(" Visual Basic") → "Visual Basic"

2 RTrim

Removes trailing whitespace from a string.

RTrim("Visual Basic ") → "Visual Basic"

3 Trim

Removes both leading and trailing whitespace.

Trim(" Visual Basic ") → "Visual Basic"

13.5 The Mid Function

The Mid function extracts a substring from a string starting at a specified position.

Syntax

Mid(string, start, [length])
Mid("Visual Basic", 3, 6) → "ual Bas"

13.6 The InStr Function

The InStr function returns the position of the first occurrence of a substring within a string.

InStrDemo.vb
Private Sub Form_Activate()
    Print InStr(1, "Visual Basic", "Basic")
End Sub

InStr Function Output:

Click "Run" to see the output

13.7 The Str and Val Functions

The Str function converts a number to a string, while Val converts a string to a number.

1 Str Function

Converts numbers to strings

Str(123.45) → "123.45"

2 Val Function

Converts strings to numbers

Val("123.45") → 123.45

13.8 The UCase and LCase Functions

These functions convert strings to uppercase or lowercase:

1 UCase

Converts to uppercase

UCase("Visual Basic") → "VISUAL BASIC"

2 LCase

Converts to lowercase

LCase("Visual Basic") → "visual basic"

13.9 The Chr and Asc Functions

The Chr function returns the character for an ASCII code, while Asc returns the ASCII code for a character.

AscChrConverter.vb
Private Sub CmdASC_Click()
    TxtASC.Text = Asc(TxtCHR.Text)
End Sub

Private Sub CmdCHR_Click()
    TxtCHR.Text = Chr(TxtASC.Text)
End Sub
ASC to CHR Converter

Figure 13.1: ASC to CHR converter application

13.10 The String Function

The String function creates a string consisting of a repeated character.

StringFunctionDemo.vb
Private Sub Form_Load()
    Form1.Show
    Print String(30, "#")
End Sub
String Function Output

Figure 13.2: Output of String function

13.11 The vbCrLf Constant

The vbCrLf constant represents a carriage return and line feed combination, used for creating new lines.

vbCrLfDemo.vb
Private Sub Command1_Click()
    Dim message As String
    Dim display As String

    message = "Mission to Mars"
    display = String(30, "*") & vbCrLf & message & vbCrLf & String(30, "*")
    MsgBox display
End Sub
vbCrLf Output

Figure 13.3: Message box using vbCrLf

13.12 Performing Word Search

Combine multiple string functions to create a word search application.

WordSearch.vb
Private Sub cmdSearch_Click()
    Dim n, m, l As Integer
    Dim myArticle, myWord As String
    myArticle = TxtArticle.Text
    myWord = TxtWord.Text
    l = Len(myWord)
    n = InStr(1, myArticle, myWord)
    If n = 0 Then
        LblResult.Caption = "Your word not found, try again."
    Else
        LblResult.Caption = "Found your word " & myWord & " at Position " & n
        TxtArticle.SetFocus
        TxtArticle.SelStart = n - 1
        TxtArticle.SelLength = Len(myWord)
    End If
End Sub
Word Search Application

Figure 13.4: Word search application

Lesson Summary

In this lesson, you've mastered VB6's essential string manipulation functions:

Len Function

Returns the length of a string

Left & Right Functions

Extract characters from the beginning or end of strings

Trim Functions

Remove whitespace from strings

Mid Function

Extracts substrings from any position

InStr Function

Finds the position of substrings

Case Conversion

UCase and LCase for text case manipulation

Chr & Asc Functions

Convert between characters and ASCII codes

Best Practice

Combine multiple string functions to solve complex text processing tasks. Always consider case sensitivity when comparing or searching strings.

Practice Exercises

Test your understanding of VB6 string functions with these exercises:

Exercise 1: Text Reverser

Create a program that reverses a string using string functions. Input: "Visual Basic" → Output: "cisalB lausiV"

Exercise 2: Word Counter

Develop a word counter that counts the number of words in a paragraph using string functions.

Exercise 3: Password Validator

Create a password validator that checks for length, uppercase, lowercase, and numeric characters.

Exercise 4: Text Formatting Tool

Build a tool that formats text by capitalizing the first letter of each sentence and trimming extra spaces.

Exercise 5: CSV Parser

Create a simple CSV parser that extracts values from a comma-separated string.

Next Lesson

Continue your VB6 journey with Lesson 14: Sub Procedures.

Related Resources

Full VB6 Tutorial Index

Complete list of all VB6 lessons with descriptions

Explore Tutorials

Visual Basic Examples

Practical VB6 code samples for real-world applications

View Examples

VB6 Formatting Functions

Learn about formatting functions in Visual Basic 6

Previous Lesson

Sub Procedures

Learn about sub procedures in VB6

Next Lesson