ASCII Character Set and String Manipulation


ASCII Character Set

Inside a computer, all characters are stored as numbers. Each programming language supports a particular set that defines valid values for characters in that language.    Visual Basic uses the ASCII character set.  Characters in the ASCII set contain all letters and symbols on the keyboard + other special characters.

Note:

  1. Capital letters are given a lower value than lower case letters. This is important when comparing strings!
  2. The first 128 characters in the ASCII set are common to all sets.
  3. A space is considered a character – given the value 32.

String Manipulation

Examples

fname = “Jon ” 
lname = “Jonsson” 
fullname = fname & lname
The value of fullname is “Jon Jonsson”
fname = “Annie” 
lname = “Buddy”
lblanswer.caption = lname & “,” & fname
The value of lblanswer.caption would be Buddy,Annie
txtname.text = “Jules” & vbCrLf & “Jurgensson”
The textbox would show: 
      Jules
      Jurgensson
The following prompts the user for their name using an inputbox: If the user does not enter a name and clicks o.k. or cancel, the value of fname will be null – an empty string! 
fname = InputBox("Please enter your name:", "Login")
 

Using String Functions

The Left, Right and Mid Functions - General Form

Left (StringExpression, NumberOfCharacters)
Right (StringExpression, NumberOfCharacters)
Mid (StringExpression, Start Position, [NumberOfCharacters] )

Examples,

Dim stExample As String
stExample = "It's a wonderful life"
lblMessage.Caption = Left (stExample, 1) ' Returns "I"
lblMessage.Caption = Left (stExample, 6) ' Returns "It's a"
lblMessage.Caption = Left (stExample, 21) ' Returns "It's a wonderful life"
lblMessage.Caption = Left (stExample, 50) ' Returns "It's a wonderful life"
lblMessage.Caption = Right (stExample, 1) ' Returns "e"
lblMessage.Caption = Right (stExample, 4) ' Returns " life"
lblMessage.Caption = Mid (stExample, 8, 9) ' Returns "wonderful"
lblMessage.Caption = Mid (stExample, 8) ' Returns "wonderful life"

The Len Function

Len (StringExpression)

Examples,

Len ("Visual Basic") ' Returns 12
Len (txtEntry.Text) ' Returns the number of characters in the text box
Len (stSelection) ' Returns the number of characters in the string variable
If Len (txtName.Text) = 0 Then
     MsgBox "Enter a name", vbInformation, "Data Missing"
End If

SelStart and SelLength Properties

    You can combine the Len function with two new properties of text boxes and the text portion of a combo box - the SelStart and SelLength properties. The SelStart property sets or returns the position of the first selected character; the SelLength property sets or returns the number of selected characters. You can make the current contents of a text box or the Text property of a combo box appear selected when the user tabs into the control and receives its focus.

Private Sub txtName_GotFocus( )
   ' Select the current entry

   
With txtName
            .SelStart = 0                                       
' begin selection at start
            .SelLength = Len (txtName.Text)     
' Select the number of characters
    End With
End Sub

Selecting Entries in a List Box

    When a list box has a very large number of entries, you can help the user by selecting the matching entry as he types in a text box. For example, when you type p the list quickly scrolls and displays words beginning with p. Then if you type r, the list scrolls down to the words that start with pr.

Private Sub txtCoffee_Change( )
   ' Locate first matching occurence in the list

   
Dim iIndex as Integer
   
Dim bFound as Boolean

    Do while Not bFound and iIndex < 1stCoffee.ListCount
            If Ucase (Left (1stCoffee.List(iIndex), Len (txtCoffee.Text))) = UCase (txtCoffee.Text) Then
                    1stCoffee.ListIndex = iIndex
                    bFound = True
            End If
            iIndex = iIndex + 1
    Loop

End Sub