You are on page 1of 8

VB Functions

VB Functions
VB Functions allows the programmer to manipulate strings in various
ways
Each VB Functions has got its special role

VB Functions: breaking up strings with
Left and Right
The Left function returns a substring from the main string starting
from the first character.
One of its parameters is how many characters to return in the
substring, two parameters are required, i.e. the string itself and the
number of characters to be returned
Syntax:
Dim Message As String
Message = Printers are not expensive
Form1.Print The first five characters are & Left(Message, 5)
Form1.Print The last 8 characters are & Left(Message, 8)

VB Functions: Removing spaces
LTrim function removes any leading spaces at the left side of a string
RTrim function removes any trailing spaces at the right side of a string
Trim does the job of both LTrim and RTrim
Syntax
The only parameter each of these functions need is the string itself
Where a user is asked to enter a surname and first name with one
space in between, you could use Ltrim and Rtrim to remove any
accidental spaces before the surname and after the first name
Using Trim would (wrongly) get rid of the spaces between the two
names also
Changing case with UCase and LCase
The UCase function converts all characters in a string into their upper
case equivalents
LCase converts all characters in a string to their lower case
The only parameter needed is the string itself.
If any of the characters are not letters they are ignored.
VB Functions: Mid function
The Mid function is used to return a single character on a given position in
a given string
Mid function can be used to concatenate strings or check if a character is
space or not among other uses
Syntax
Three parameters are required, the string, position, number of characters
to pick)
Question
Ask a user to enter a string and then tell them whether or not it is a
palindrome.
A palindrome is a string that is the same backwards and forwards, for
example, star rats or level

VB Functions: StrReverse
The StrReverse function returns the given string in reverse form, for
example, the is returned as eht
This function can be used to check whether a given string is a
palindrome
A palindrome is a string that is the same backwards and forwards, for
example, star rats or level
The only parameter is the string itself
Question:
Ask a user to enter a string and then tell them whether or not it is a
palindrome.

Summary
To return characters from a string from left or from right use Left or
Right functions respectively
Remove spaces from strings by Trim, LTrim and RTrim
Use UCase and LCase to change case of strings
Use StrReverse to return strings in reverse form
Use Mid function to process individual characters in strings

You might also like