The VBScript Len() function returns the number of characters in a string. It counts spaces, punctuation and line-break characters as well as letters and numbers. In Classic ASP, use Len() for string-length checks and LenB() when you need to understand the bytes used to represent a string internally.
Len("Hello") returns 5. For a Unicode VBScript string, LenB("Hello") typically returns 10; that is not the UTF-8 size of an HTML response.<%
Option Explicit
Dim myString
myString = "Welcome to Plus2net.com"
Response.Write "Characters: " & Len(myString)
%>
Output: Characters: 23. The string contains letters, a period and spaces, all of which contribute to the count.
Len(stringExpression)
LenB(stringExpression)
| Function | What it returns for a string | Common use |
|---|---|---|
Len() | String length in VBScript characters/code units | Text limits, blank input checks, substring processing |
LenB() | Number of bytes used in the string's internal representation | Inspecting byte-oriented string storage behavior |
Both functions accept an expression or variable. Unlike LenB(), Len() should generally be your default when checking the length of user-entered text.
The following example keeps the original tutorial's my_string value and displays its length with Response.Write.
<%
Dim my_string
my_string = "Welcome to Plus2net.com"
Response.Write Server.HTMLEncode(my_string)
Response.Write "<br>Length of this string = " & Len(my_string)
%>
Welcome to Plus2net.com
Length of this string = 23
Use Server.HTMLEncode() when displaying text that could contain HTML-sensitive characters. It prevents the browser from interpreting those characters as markup.
Len() counts whitespace even though normal HTML display collapses a run of consecutive spaces. Here the original example has three spaces before the first letter:
<%
Dim my_string
my_string = " Welcome to Plus2net.com"
Response.Write Server.HTMLEncode(my_string)
Response.Write "<br>Length of this string = " & Len(my_string)
%>
Welcome to Plus2net.com
Length of this string = 26
The three leading spaces are present in the string and count toward 26 characters, even if the browser does not visually show them. If you need to display whitespace exactly, use a CSS style such as white-space: pre-wrap.
<%
Response.Write Len("A B") & "<br>" ' 3 characters
Response.Write Len("A B") ' 4 characters
%>
Notice that the two spaces between A and B are both counted.
Use Trim() when you want to ignore leading and trailing spaces before applying a maximum or minimum length rule. Len() alone does not remove them.
<%
Dim message
message = " Hello ASP "
Response.Write "Original: " & Len(message) & "<br>"
Response.Write "After Trim: " & Len(Trim(message)) & "<br>"
Response.Write "After LTrim: " & Len(LTrim(message)) & "<br>"
Response.Write "After RTrim: " & Len(RTrim(message))
%>
Output: Original: 15; After Trim: 9; After LTrim: 12; After RTrim: 12. Each of these functions removes spaces from the corresponding end of the string before Len() counts it.
<%
Response.Write Len("") ' 0
%>
An empty string has length 0. You can also test Len(Trim(value)) = 0 when checking whether a visitor entered only spaces.
<%
Dim note
note = "A" & vbCrLf & "B"
Response.Write Len(note) ' 4: A + CR + LF + B
%>
In this example, vbCrLf contains two control characters: carriage return and line feed. They add two to the length, even though they appear as one line break when displayed.
The original tutorial also compares Len() and LenB(). VBScript stores normal string values using a Unicode representation, so the byte-oriented result can be larger than the character count.
<%
Dim my_string
my_string = "Welcome to Plus2net.com"
Response.Write Server.HTMLEncode(my_string)
Response.Write "<br>Length of this string = " & Len(my_string)
Response.Write "<br>Byte-oriented length = " & LenB(my_string)
%>
Welcome to Plus2net.com
Length of this string = 23
Byte-oriented length = 46
46 describes the typical in-memory representation of this VBScript string. LenB() is not a reliable way to calculate the number of UTF-8 bytes in a web response, database field or uploaded file. Do not assume every visible Unicode symbol occupies exactly one VBScript character.<%
Dim sample
sample = "ABC"
Response.Write "Len: " & Len(sample) & "<br>"
Response.Write "LenB: " & LenB(sample)
%>
Typical output: Len: 3 and LenB: 6. The byte result reflects the internal string representation, not the encoded HTTP payload.
If you need the actual byte count of a file or transmitted payload, measure its bytes after using the relevant encoding. A character-count limit and a byte-size limit solve different problems.
A common use of Len() is validating text received through Request.Form. This complete example can be placed in a .asp file for testing on an IIS server.
<%
Option Explicit
Dim userName
userName = ""
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
userName = Trim(Request.Form("username"))
If Len(userName) = 0 Then
Response.Write "Please enter a name."
ElseIf Len(userName) > 50 Then
Response.Write "Name must be 50 characters or fewer."
Else
Response.Write "Hello, " & Server.HTMLEncode(userName)
End If
End If
%>
<form method="post" action="">
<label for="username">Your name</label>
<input type="text" id="username" name="username" maxlength="50" required>
<button type="submit">Submit</button>
</form>
The HTML maxlength setting helps visitors, but the server-side length check is still needed because client-side rules can be bypassed. This example covers simple name-length validation; production input processing may require additional validation for its intended purpose.
<%
Dim searchText
searchText = Trim(Request.QueryString("q"))
If Len(searchText) < 3 Then
Response.Write "Enter at least 3 characters to search."
Else
Response.Write "Search text: " & Server.HTMLEncode(searchText)
End If
%>
This example uses a GET query parameter; it can be tested with a URL ending in ?q=asp. Use additional validation and appropriate parameterized queries before passing submitted data to a database.
Len() provides the upper limit when extracting individual characters with Mid():
<%
Dim word, i
word = "ASP"
For i = 1 To Len(word)
Response.Write Mid(word, i, 1) & "<br>"
Next
%>
Output: A, S and P on separate lines. Remember that character positions used by Mid() start at 1, even though many programming languages use zero-based array indexes.
Null is different from an empty string. Len(Null) returns Null, not the numeric value 0. Check for Null before relying on a numeric length comparison.
<%
Dim value
value = Null
If IsNull(value) Then
Response.Write "No value available"
Else
Response.Write "Length: " & Len(value)
End If
%>
For an ordinary empty string, Len("") returns zero. If the source may contain database Null values, handle them deliberately rather than assuming they behave like empty text.
The string may contain leading or trailing spaces, repeated spaces, tabs or line breaks. Browsers collapse some whitespace visually, but VBScript still counts the characters in the underlying string.
Yes. Spaces inside the string count, including spaces at the beginning and end. Use Trim() if you want to remove the outer spaces first.
No universal relationship should be assumed for arbitrary data or encodings. The common VBScript Unicode string representation often produces a two-to-one result for the simple examples shown here; this does not describe UTF-8 storage or the number of visible Unicode characters.
Yes. Use Len(Trim(Request.Form("field"))) for a simple text field, with server-side minimum or maximum length checks. Also encode user-submitted text before displaying it in HTML.
This PHP tutorial displays Classic ASP code as examples. To execute it, copy the code into a file with an .asp extension and run it on a server that supports Classic ASP, such as a properly configured IIS server.
No. File size is measured in bytes, and text length depends on representation and encoding. For file inspection in Classic ASP, see FileSystemObject file handling.
Continue learning string functions and form processing with these related pages:
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.