String Function

String functions:

There are many useful string functions in Visual Basic 6. The string functions are defined in the "Strings" module. Search in the object browser.
The string functions are explained below:


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
10 spaces before 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

No comments:

Post a Comment