Outline

 What are strings
 Left,Mid,Right functions
 

Your current position:
Home 
  Visual Basic
    Programming Basics
      Strings

Strings

 

First, what are strings? Strings are variables that contain text.

In Visual Basic there are two types of strings:

  • Fixed-length strings (approximately 2 billion (2^31) characters)

  • Variable-length strings  (approximately 64K (2^16) characters)

The codes for String characters range from 0–255. The first 128 characters (0–127) of the character set correspond to the letters and symbols on a standard U.S. keyboard. These first 128 characters are the same as those defined by the ASCII character set. The second 128 characters (128–255) represent special characters, such as letters in international alphabets, accents, currency symbols, and fractions. The type-declaration character for String is the dollar sign ($).

Manipulating Strings

 

Left, Mid, Right functions

 

The Left, Mid and Right are used for retrieving certain characters from a string. Depending on the position of the characters you will choose the needed function:

Use the Left function to get the beginning characters of your string, the Right function for the characters from the end and the Mid function for characters form the middle, beginning and end.

Syntax:

 

[the characters] = Left ( [your string],[number of characters] )

[the characters] = Right ( [your string],[number of characters] )

[the characters] = Mid ( [your string],[the beginning character],[number of characters] )

 

Example:

 

 Dim MyString As String
 MyString = "Left, Mid and Right functions demo"
 Debug.Print Left(MyString, 4) ' prints "Left" to imediate window
 Debug.Print Right(MyString, 4) 'prints "demo" to immediate window
 Debug.Print Mid(MyString, 7, 3) 'Prints "Mid" to immediate window
 Debug.Print Mid(MyString, 1, 4) 'prints "Left" to imediate window
 Debug.Print Mid(MyString, 31, 4) 'prints "demo" to imediate window