The VB Function for a String Length

Visual Basic functions are a way of formatting code so it performs a specific task and always return a value. In the case of strings -- or series of character data -- VB provides 14 function options from which to choose. One of these helps you determine the length of a string. While you may not use this VB function often, it can be helpful for customizing data display and for determining data storage needs.

  1. Identification

    • The VB function for a string length is the LEN function. While only Visual Studio provides subcategories that expand its usability, the LEN function applies whether you are working with VBScript on a website, using Visual Basic for Applications (VBA) on a Microsoft Office product such as Excel, or creating a full program in Microsoft Visual Studio. If you plan to use the LEN function in a Visual Studio program, you can ensure the function performs exactly as your program requires by choosing from one of 16 LEN subcategories.

    Format

    • The correct grammar (syntax) for the LEN function is the same for all versions of VB, although additional code that declares and defines the variable is different for each. VBScript starts with the string you want to evaluate in the first line. An example is txt="This is a beautiful day!" Code in the second line “document.write(Len(txt))” to display a numerical value for the number of characters in the string. In a VBA macro, use a line of code such as “Dim LResult As Long” to declare and define the string variable and include the LEN function on the second line with code such as LResult = Len ("This is a beautiful day!") Code in a Visual Studio program can be complex and include numerous lines, but the syntax for the LEN function is the same.

    Usage

    • While the idea of using the LEN function as a way of determining storage needs may be easy to understand, it can be more difficult to understand how this function can have a practical use. You can use the LEN function in a practical way -- usually in combination with other string functions -- to divide or parse long strings of character data and populate tables, columns or spreadsheet cells. Another practical use is to include the LEN function as a validation method. For example, you can include the LEN function to make sure a user enters a nine-digit Social Security number in an input box or alternately allow a user to leave an input box blank.

    Illustration

    • The VB LEN function is a less commonly used function. While it is important to understand and is part of introductory VB training, you will find it most useful as a small portion of large blocks of code rather than useful on its own.

Related Searches

References

  • Photo Credit Goodshoot/Goodshoot/Getty Images

Related Ads

Check It Out

Should Microsoft Be Allowed to Force You to Upgrade?