Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with:

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

Posted by Ben Nadel
Tags: ColdFusion

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. --->
  • strText,
  •  
  • <!--- The index AFTER which we insert the new string. --->
  • REFind( "(?i)\sfeet", strText )
  • ) />
  •  
  • <!--- Output the string. --->
  • #strText#

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




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

Reply to this Comment

Ben,

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

Mike

Reply to this Comment

@Mike,

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

Reply to this Comment

Post A Comment

?
You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.