ColdFusion Tutorial

Remove leading and trailing spaces from a string using Trim, LTrim and RTrim.

Sometimes it is useful to remove leading and trailing spaces from a string.
Example: When saving a string to a database.

LTrim
LTrim function removes leading spaces from the string.

RTrim
RTrim function removes spaces from the end of a string.

Trim
Trim function removes leading and trailing spaces from a string.

The example also uses the Len function.
Len function return the length of the string expressed as a number.

Example 1 uses LTrim and RTrim.


<cfset the_string = "    This example removes leading and ending spaces from a string.    ">
Output the original string:
<cfoutput>#the_string#</cfoutput><br>
<!--- Len function return the length of the string expressed as a number. --->
The original length of the string:
<cfoutput>#Len(the_string)#</cfoutput><br>
<!--- LTrim function removes leading spaces from the string. --->
<cfset the_string = LTrim(the_string)>
<!--- Find the length of the string after using LTrim. --->
Output the length of the string after using the LTrim function:
<cfoutput>#Len(the_string)#</cfoutput><br>
<!--- RTrim function removes spaces from the end of a string. --->
<cfset the_string = RTrim(the_string)>
<!--- Find the length of the string after using RTrim. --->
Output the length of the string after using the RTrim function:
<cfoutput>#Len(the_string)#</cfoutput>

Output from example 1:

Output the original string: This example removes leading and ending spaces from a string.
The original length of the string: 69
Output the length of the string after using the LTrim function: 65
Output the length of the string after using the RTrim function: 61


Example 2 use Trim

<cfset the_string = "    This example removes leading and ending spaces from a string.    ">
Output the original string:
<cfoutput>#the_string#</cfoutput><br>
<!--- Len function return the length of the string expressed as a number. --->
The original length of the string:
<cfoutput>#Len(the_string)#</cfoutput><br>
<!--- Trim function removes leading and trailing spaces from a string. --->
<cfset the_string = Trim(the_string)>
Output the length of the string after using Trim:
<cfoutput>#Len(the_string)#</cfoutput>

Output from example 2:

Output the original string: This example removes leading and ending spaces from a string.
The original length of the string: 69
Output the length of the string after using Trim: 61

ColdFusion Tutorial test

Remove leading and trailing spaces from a string using Trim, LTrim and RTrim.

No test is available or implemented for this ColdFusion Tutorial.

No User Comments.

No User Comments, be the first one to write your comments?

Add your comments

Your Name*:
Your Email:
Site URL:
Site Name:
Comemt Title*:
Your Comment*:
Key Phrase*:
L8p
Your IP Address: 38.103.63.62
 

ScandicWeb is own by Scandic Systems LTD [UK] Company No. 5984000. All other trademarks and copyrights are the property of their respective holders.

[Home [Site Map [Privacy Statement [Site Feedback] [About ScandicWeb]


[ColdFusion [Flash [Search Engine Optimization [Graphics [Cascading Style Sheets (CSS) [JavaScript (JS) [Software]

Partner sites: ScandicSoft


Server time: 19. November 2008 Wednesday