View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > Trim PreviousNext

Trim

Removes leading and trailing spaces from a string.

A copy of string, after removing leading and trailing spaces.

String functions

Trim(string)

LTrim, RTrim

Parameter

Description

string

A string or a variable that contains one

<h3>Trim Example</h3>
<cfif IsDefined("FORM.myText")>
   <cfoutput>
      <pre>
         Your string:   "#FORM.myText#"
         Your string:   "#Trim(FORM.myText)#"
         (trimmed on both sides)
      </pre>
   </cfoutput>
</cfif>
<form action = "trim.cfm">
<p>Type in some text, and it will be modified by trim to remove leading 
spaces from the left and right <p><input type = "Text" name = "myText" value = " TEST "> <p><input type = "Submit" name = ""> </form>

Contents > CFML Reference > ColdFusion Functions > Trim PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


Belikov said on Aug 3, 2004 at 11:12 AM :
Trim in fact removes not only spaces. I found some other characters that Trim removes. Please consider the following code:

<cfset mdWddx = " "&chr(10)&chr(13)&chr(9)&chr(7)&chr(11)&" ">
<cfoutput><br>len = #len(mdWddx)#</cfoutput>
<cfloop index="i" from="1" to="#len(mdWddx)#">
<cfoutput><br>character #i# is #asc(mid(mdWddx,i,1))#</cfoutput>
</cfloop>
<cfset mdWddx=trim(mdWddx)>
<cfoutput><br>len = #len(mdWddx)#</cfoutput>
<cfloop index="i" from="1" to="#len(mdWddx)#">
<cfoutput><br>character #i# is #asc(mid(mdWddx,i,1))#</cfoutput>
</cfloop>

The result is:
len = 7
character 1 is 32
character 2 is 10
character 3 is 13
character 4 is 9
character 5 is 7
character 6 is 11
character 7 is 32
len = 0

Is it a bug or a feature?
MikerRoo said on Feb 18, 2005 at 2:07 AM :
This does NOT, however, get rid of Chr (160) which is unicode for &nbsp;.
This can be a gotcha when processing text inputs.
mysorian said on Sep 8, 2005 at 6:27 AM :
Need to exercise caution while using special characters:
<cfset wrd1="&nbsp;">
<cfset lnwrd1=Len(#wrd1#)>
<cfset lngth1=trim(#lnwrd1#)>
<cfset wrd2=" &nbsp; ">
<cfset lnwrd2=Len(#wrd2#)>
<cfset lngth2=trim(#lnwrd2#)>
<cfoutput>
wrd1 is : #wrd1#
<br>
lnwrd1 is: #lnwrd1#
<br>
lngth1 is: #lngth1#
<br>
wrd2 is: #wrd2#
<br>
lnwrd2 is: #lnwrd2#
<br>
lngth2 is: #lngth2#
<!---
-----output---
wrd1 is :
lnwrd1 is: 6
lngth1 is: 6
wrd2 is:
lnwrd2 is: 8
lngth2 is: 8--->

</cfoutput>

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/funca111.htm