Trim Spaces from Strings in Classic ASP

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.

Syntax of Trim(), LTrim() and RTrim()

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)
FunctionWhat it removesExampleResult
LTrim()Spaces at the beginningLTrim(" ASP ")"ASP "
RTrim()Spaces at the endRTrim(" ASP ")" ASP"
Trim()Spaces at both endsTrim(" ASP ")"ASP"

The string argument can be a literal string, a variable, or an expression returning a string.

LTrim(): remove leading spaces

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 & "|"
%>

Output

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(): remove trailing spaces

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|.

Trim(): remove spaces from both ends

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)

Compare all three functions with one 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))
%>

Output

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.

Trim() does not remove spaces inside a string

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.

Replace a known number of internal spaces

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.

Trim values submitted from an HTML form

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.

Sample HTML form

<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>

Read and validate in process.asp

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.

Important: trimming is not a security filter. It does not prevent SQL injection or HTML injection. Validate values for their intended purpose, use parameterized database queries, and HTML-encode untrusted values when displaying them in HTML.

Trim individual elements after Split()

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
%>

Output

apple
banana
orange

Compare this with Split() in Classic ASP, or use Join() to recombine array elements.

Empty strings and Null values

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
%>

Handle Null before using the result

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.

Spaces versus tabs and other whitespace

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.

Remove tab characters when they are unwanted everywhere

<%
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.

Common mistakes and troubleshooting

ProblemReasonFix
Spaces still appear between wordsTrim() removes only outer spaces.Handle internal separators separately, for example with Replace().
Whitespace is not visibly changingHTML collapses repeated spaces when rendering text.Compare lengths with Len() or show markers such as |.
Original variable still has spacesThe result was not assigned back.Use my_string = Trim(my_string).
Tab or newline remainsIt is not an ordinary space.Identify the actual character and handle it appropriately.
Unexpected Null valueTrim(Null) returns Null.Check with IsNull() before using the value.
Unexpected or unsafe output from a formTrimming does not validate or encode content.Validate the value and use Server.HTMLEncode() for HTML output.

Frequently asked questions

What is the difference between Trim(), LTrim() and RTrim()?

Trim() removes spaces from both the beginning and end, LTrim() removes them from the beginning, and RTrim() removes them from the end.

Does Trim() remove spaces between words?

No. It only removes leading and trailing spaces. The spaces inside the string remain.

Will Trim() change the original variable?

Not unless you assign its return value: my_string = Trim(my_string).

What happens when Trim() receives an empty string?

It returns an empty string. For a string containing only ordinary spaces, the result is also an empty string.

Can I use Trim() with Request.Form values?

Yes. It is useful for removing accidental surrounding spaces before applying server-side validation. It is not a substitute for validation or output encoding.

Does Trim() remove tabs and line breaks?

Do not rely on it for that purpose. These functions target space characters; use input-specific handling for other whitespace.

What does Trim() return for Null?

It returns Null. Check with IsNull() when the argument could be Null.

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer