How can I ensure there are no leading or trailing spaces in my variable?

When working with data that you have no control over, you sometimes need to do a bit of cleanup on strings. For example, a web form that accepts user input may send data with extra spaces at the beginning or end of the value.

ColdFusion provides three functions to address the issue of leading or trailing spaces in ColdFusion: trim(), rTrim(), and lTrim(). The functions rTrim() and lTrim() will strip spaces from the end or beginning of a string. Normally you want to strip white space from both the beginning and end of a string. For that you would use the trim() function.

<cfset str = "  Jacob turned six years old on Wednesday          ">
<cfset str = trim(str)>

This question was written by Charlie Griefer
It was last updated on January 27, 2006.


Display and Layout


comments powered by Disqus