Visual Basis: Count Number of Characters in a String

Count Number of Characters in a String

This is a visual basic 6.0 source code to count the number of characters in a string. This function can be useful in Text editors or where you want to tell the user that how many characters have been inputted in the text field. You can also define which characters to count and which do not.

Public Function CountCharacters(ByVal Text As String) As Integer
    Dim counter%, t%
    Const Characters$ = "abcdefghijklmnopqrstuvwxyz"

    For t% = 1 To Len(Text)
        If InStr(Characters, LCase$(Mid$(Text, t%, 1))) <> 0 Then
            counter% = counter% + 1
        End If
    Next t%
    CountCharacters = Counter%
End Function

To count the number of characters in a string using Visual Basic, you can also use the Len function. Here is an example code snippet:

Dim myString as String
myString = "Hello, world!"
Dim stringLength as Integer
stringLength = Len(myString)
MsgBox("The length of the string is " & stringLength)

In this Visual Basic code, the myString variable contains the string you want to count the characters in. The Len function is used to determine the length of the string, which is then stored in the stringLength variable. Finally, a message box is displayed that shows the count of characters in the string.

Note that the Len function counts all characters in the string, including spaces and punctuation. If you want to exclude certain characters from the count, you will need to modify the code accordingly as showing in the first program.

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post