Use Trim() to remove spaces from both ends of a string. Use LTrim() for spaces at the beginning and RTrim() for spaces at the end.
<%
Dim my_string
my_string = " Welcome to Plus2net.com "
Response.Write "|" & Trim(my_string) & "|"
%>
Output: |Welcome to Plus2net.com|. The vertical bars make it easy to see that there are no spaces before or after the result.
All three VBScript functions return a new string value. They do not change an existing variable unless you assign the result back to it.
Trim(string)
LTrim(string)
RTrim(string)
| Function | What it removes | Example | Result |
|---|---|---|---|
LTrim() | Spaces at the beginning | LTrim(" ASP ") | "ASP " |
RTrim() | Spaces at the end | RTrim(" ASP ") | " ASP" |
Trim() | Spaces at both ends | Trim(" ASP ") | "ASP" |
The string argument can be a literal string, a variable, or an expression returning a string.
This original example removes the single leading space from my_string. The value is assigned back to the same variable so later code uses the cleaned text.
<%
Dim my_string
my_string = " Welcome to Plus2net.com"
Response.Write "Before removing space =|" & my_string & "|"
my_string = LTrim(my_string)
Response.Write "<br>After removing space =|" & my_string & "|"
%>
Before removing space =| Welcome to Plus2net.com|
After removing space =|Welcome to Plus2net.com|
The | characters reveal the leading space in the first line. Without a marker, consecutive spaces can be difficult to see in a browser.
RTrim() removes spaces at the end of a string, while leaving any spaces at the beginning intact.
<%
Dim my_string
my_string = " Welcome to Plus2net.com "
Response.Write "Before: |" & my_string & "|<br>"
my_string = RTrim(my_string)
Response.Write "After: |" & my_string & "|"
%>
Output: The second line retains two leading spaces but removes the three trailing spaces: | Welcome to Plus2net.com|.
Use Trim() when you do not want any ordinary spaces before or after the string.
<%
Dim my_string
my_string = " Welcome to Plus2net.com "
my_string = Trim(my_string)
Response.Write "|" & my_string & "|"
%>
Output: |Welcome to Plus2net.com|. You can also combine LTrim() and RTrim(), but one call to Trim() is simpler.
my_string = LTrim(RTrim(my_string)) ' Same edge-space result as Trim(my_string)
This example lets you run all three functions against the same original string and compare the results. It uses Len() to make the difference measurable.
<%
Dim my_string
my_string = " ASP "
Response.Write "Original length: " & Len(my_string) & "<br>"
Response.Write "LTrim length: " & Len(LTrim(my_string)) & "<br>"
Response.Write "RTrim length: " & Len(RTrim(my_string)) & "<br>"
Response.Write "Trim length: " & Len(Trim(my_string))
%>
Original length: 7
LTrim length: 5
RTrim length: 5
Trim length: 3
Len() counts spaces, even when the browser does not visibly show them. The original string has two spaces before ASP and two after it. Learn more in the VBScript string-length tutorial.
It is common for text to contain extra spaces between words. Trim() only removes spaces at the two ends; it does not compress spaces in the middle.
<%
Dim textValue
textValue = " Learn Classic ASP "
Response.Write "|" & Trim(textValue) & "|"
%>
Output: |Learn Classic ASP| (three spaces remain between the words). If you specifically need to replace repeated internal spaces, use Replace(), covered in the VBScript string replacement tutorial.
Here, the separator consists of exactly three spaces. We replace that three-space sequence with one space, then trim the ends.
<%
Dim textValue
textValue = " Learn Classic ASP "
textValue = Replace(textValue, " ", " ")
Response.Write Trim(textValue)
%>
Output: Learn Classic ASP. This is a simple example for a known separator, not a general-purpose Unicode whitespace normalizer.
Leading and trailing spaces often enter an application through text fields. Use Trim(Request.Form("name")) to normalize a form value before checking whether it is empty.
<form method="post" action="process.asp">
<label for="displayName">Your name</label>
<input type="text" id="displayName" name="displayName" required>
<button type="submit">Submit</button>
</form>
The following code belongs in the receiving process.asp file. Output is HTML-encoded when it includes user-submitted text.
<%
Dim displayName
displayName = Trim(Request.Form("displayName"))
If Len(displayName) > 0 Then
Response.Write "Welcome, " & Server.HTMLEncode(displayName)
Else
Response.Write "Please enter your name."
End If
%>
Input containing only spaces becomes an empty string, so the Else branch runs. The HTML required attribute improves browser usability, but the ASP validation is still necessary. For more examples see Classic ASP form handling and validating user input.
When a simple comma-separated string contains spaces around its values, trim the parts individually. This example is intended for a basic delimiter-separated list, not complex quoted CSV files.
<%
Dim names, items, item
names = " apple , banana, orange "
items = Split(names, ",")
For Each item In items
Response.Write Server.HTMLEncode(Trim(item)) & "<br>"
Next
%>
apple
banana
orange
Compare this with Split() in Classic ASP, or use Join() to recombine array elements.
Trimming an empty string returns another empty string. A Null value is different: Trim(Null) returns Null, rather than a usable empty string.
<%
Dim inputText
inputText = " "
Response.Write "Length: " & Len(Trim(inputText)) ' Prints 0
%>
If a value can be Null, test it with IsNull() before using it in text operations that require a string.
<%
Dim inputText
inputText = Null
If IsNull(inputText) Then
Response.Write "No text supplied"
Else
Response.Write Trim(inputText)
End If
%>
For ordinary string input that could be blank, Len(Trim(value)) = 0 is a useful check, once you know the value is not Null.
VBScript Trim(), LTrim() and RTrim() are designed to remove ordinary space characters at the edges. Do not assume they remove tab characters, carriage returns, line feeds, or every Unicode whitespace character.
For example, vbTab is a tab character, not a space. If a particular input source includes tabs, handle that input deliberately rather than assuming Trim() is a universal whitespace-cleaning function.
<%
Dim textValue
textValue = vbTab & " ASP " & vbTab
textValue = Replace(textValue, vbTab, "")
Response.Write "|" & Trim(textValue) & "|"
%>
Output: |ASP|. This use of Replace() removes all tab characters, including any in the middle of the string. Use it only if that is what your application needs.
| Problem | Reason | Fix |
|---|---|---|
| Spaces still appear between words | Trim() removes only outer spaces. | Handle internal separators separately, for example with Replace(). |
| Whitespace is not visibly changing | HTML collapses repeated spaces when rendering text. | Compare lengths with Len() or show markers such as |. |
| Original variable still has spaces | The result was not assigned back. | Use my_string = Trim(my_string). |
| Tab or newline remains | It is not an ordinary space. | Identify the actual character and handle it appropriately. |
Unexpected Null value | Trim(Null) returns Null. | Check with IsNull() before using the value. |
| Unexpected or unsafe output from a form | Trimming does not validate or encode content. | Validate the value and use Server.HTMLEncode() for HTML output. |
Trim() removes spaces from both the beginning and end, LTrim() removes them from the beginning, and RTrim() removes them from the end.
No. It only removes leading and trailing spaces. The spaces inside the string remain.
Not unless you assign its return value: my_string = Trim(my_string).
It returns an empty string. For a string containing only ordinary spaces, the result is also an empty string.
Yes. It is useful for removing accidental surrounding spaces before applying server-side validation. It is not a substitute for validation or output encoding.
Do not rely on it for that purpose. These functions target space characters; use input-specific handling for other whitespace.
It returns Null. Check with IsNull() when the argument could be Null.
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.