VB6 Tutorial 37: String Functions

Posted On 2018-04-20 by VB6Boy
Keywords:
Tags: VB6 Tutorials 
Views: 34


Looking for the more of the VB6 Tutorial? Here is the tutorial index.

There are many useful string functions in Visual Basic 6. The string functions are defined in the "Strings" module, which can be found in the Object Browser.

Left

The Left function extracts characters from the left of the string.

Syntax

Left(string, n)  

n is the number of characters to be extracted.

Example

Print Left("Visual", 3)

Output: Vis

Right

The Right function extracts characters from the right of the string.

Syntax

Right(string, n)

string is the character string, n is the number of characters to be extracted.

Example

Print Right("Visual", 3)

Output: ual

Mid

Use the the Mid function to extract characters from the middle of the string.

Syntax

Mid(string, m, n)

string is the character string, the extracted string will begin at mth character, n is the number of characters to be extracted.

Example

Print Mid("Visual", 3, 2)

Output: su

Len

The Len function calculates the number of characters in a string.

Example

Private Sub cmdCount_Click()
    Dim str As String
    str = "Visual Basic"
    Print Len(str)
End Sub

Output: 12

InStr

The InStr function returns the position of first occurrence of a sub-string contained in a string.

Syntax

Instr(str1, str2)

Example

Dim str As String
str = "Visual Basic"
Print InStr(str, "Basic")

Output: 8

Chr & Asc

The Chr function converts an ASCII value to character and Asc function converts the character to ASCII.

Syntax

Chr(ASCII value)

Asc(chr)

Example

Print Chr(65)

Output: A

Example

Print Asc("A")

Output: 65

Chr(9) and chr(13) are the Tab and Carriage-return characters respectively.

Example

Print "Hello" & Chr(9) & "world"

Output: Hello     world

Print "Hello" & Chr(13) & "world"

Output:

Hello
world

Str

The Str function returns the string representation of a number.

Syntax

Str(n) 

Example

Print str(5) + str(7)

Output: 57

LCase & UCase

The LCase function converts an upper case string to lower case and the UCase converts a lower case string to upper case.

Example

Private Sub cmdChangeCase_Click()
    Dim s As String
    s = "HELLO"
    Print LCase(s)
End Sub

Output: hello

Example

Dim s As String
s = "hello"
Print UCase(s)

Output: HELLO

Trim

The Trim function returns a string without leading and trailing spaces.

Example

Dim s As String
s = "                      HELLO"
Print Trim(s)

Output:
HELLO

LTrim & RTrim

The Ltrim function trims leading spaces and the RTrim function trims trailing spaces from a string.

StrReverse

The StrReverse function reverses a string.

Example

Print StrReverse("Basic")

Output: cisaB

Space

Space function returns a specific number of spaces.

Example

Print Space(10) + StrReverse("Basic")

Output:

          Basic

StrConv

The StrConv function converts the string into a particular case, e.g upper case, lower case and proper case.

Example

Print StrConv("hello", vbUpperCase)

Output: HELLO

Print StrConv("hello", vbProperCase)

Output: Hello

StrComp

The StrComp function compares two strings to check whether they match or not. It returns 0, if the two strings match with one another.

str = StrComp("vb", "vb", vbTextCompare) 'Returns 0
str = StrComp("vb", "VB", vbTextCompare) 'Returns 0 
str = StrComp("vb", "VB", vbBinaryCompare) 'Returns 1

If the optional argument is vbTextCompare then this is a case insensitive comparison, if vbBinaryCompare then it's a case sensitive comparison.

Tab

The Tab(n) function lets the string be displayed at nth position.

Example

Print Tab(20) ; "Language"

Output:

                    Language

Replace

The Replace function finds a sub-string in a string and replaces the sub-string with another.

Example

newvalue = Replace("months", "s", "1") 'Returns month1

Looking for the more of the VB6 Tutorial? Here is the tutorial index.


About the Author

has posted a total of 78 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.166.212.152

Before you can post, you need to prove you are human. If you log in, this test goes away.


Code Links