CyberiaPC.com Main Page




Learning VB

Manipulating Strings and Integers

There are many small things that can be done to data types (integers, strings...) in VB that make the life of the programmer much easier.  In this section, I'll discuss the most important and the most widely used techniques for manipulating numbers and characters.  Here is an index of what will be discussed in this lesson:

 
Converting Data Types
Constants
Dates and Times
Using the Format Function
Manipulating Text Strings

Manipulating Text Strings

In addition to manipulating numbers and formatting them in different ways, VB can also manipulate strings.  For example, the programmer might want to extract only the first part of a user's full name, or maybe check the length of a string and then display message boxes depending on the length.  All this and more can be achieved by using the functions shown below:

Action Function
Compare two strings StrComp
Convert to lowercase or uppercase Format, LCase, Ucase
Create string of repeating character Space, String
Find length of a string Len
Format a string Format
Justify a string LSet, RSet
Manipulate strings InStr, InStrRev, Left, LTrim, Mid, Right, RTrim, Trim
Convert strings StrConv

For example, to get the length of the string MyName and store it in another variable, intLength, declared as integer:

     intLength = Len(MyName)

The Left is a function that you might find yourself using often, it simply finds the number of characters from the left side of a string.  Right, does the exact opposite.

     Left(MyName, 4)

This example displays only the first 7 characters of the string MyName:

     Dim MyName As String
     Dim NewMyName As String
     
     MyName = "My Name is Me!"
     NewMyName = Left(MyName, 7)
     
     MsgBox NewMyName

This displays: "My Name" which it extracts from the complete string "My Name is Me!"

To explain the other functions would take up a lot of space in this lesson, therefore, you are advised to check your VB documentation or the Microsoft web site for detailed information about each of the other functions that were not discussed here.

{Go Back To VB Lessons Page}