Skip to main content
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Michelle Meadows
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Michelle Meadows

ColdFusion Insert() - How Did I Miss This Function?

Published in Comments (5)

I was doing some code evaluation today and I came across someone who was using a method named Insert(). At first I just assumed that this person was using a UDF since the method didn't look familiar to me. But, after checking out my HomeSite Help files, I realized that Insert() was a built-in ColdFusion function. How the heck did I miss this?!? Granted, I am not sure how often I would use something like this, but certainly, I am at the point where I should have all these low-level utility functions committed to memory.

The ColdFusion Insert() method takes a substring, a string, and an index and inserts the given substring into the given string after the given index. If the index is zero, the substring is prepended to the string. Take a look at this example:

<!--- Create a string. --->
<cfset strText = "Mind if I massage your feet?" />

<!--- Insert the term "sexy" before the word feet. --->
<cfset strText = Insert(
	<!--- The string we are inserting. --->
	"sexy ",

	<!--- The string we are modifying. --->

	<!--- The index AFTER which we insert the new string. --->
	REFind( "(?i)\sfeet", strText )
	) />

<!--- Output the string. --->

Running the above code gives us the following output:

Mind if I massage your sexy feet?

In my example, I am using ColdFusion's REFind() method to find the index at which to insert. Since Insert() inserts the substring AFTER the index, I am including the space (\s) in my regular expression. This example is simple, but it is not a great example - if REFind() failed to find a match, it would return zero. Zero, in our case would not hault the Insert() method, but instead, it would prepend the substring to the string - probably not something we would intend on doing.

Anyway, I hate it when functions like this either completely slide under my radar or at least don't get stuck in my memory. Sloppy, sloppy, sloppy :)

Want to use code from this post? Check out the license.

Reader Comments


Ha ha ha ha... Charlie, I think I even remember reading that post. I remember the phrase "hahaha. silly n00b"... Dang! That was months ago. Why can't I insert... Insert into my brain!!!

That's it! I'm using Insert() somehow in every algorithm going forward until it sticks :)



Can you update this post with an example of comparing two comma separated lists and using "Insert()" to make the lists equal?




You could probably just convert both lists to arrays and then using arrayInsertAy(). But, I am not sure I fully understand the question.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel