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

Posted July 16, 2007 at 5:35 PM 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

Jul 16, 2007 at 6:31 PM // reply »
25 Comments

heh...

http://cfblog.griefer.com/index.cfm/id/insert_i_had_no_idea

:)


Jul 16, 2007 at 6:39 PM // reply »
17 Comments

Ha Charlie!

I KNEW I'd seen this post before!


Jul 16, 2007 at 8:05 PM // reply »
11,238 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 :)


Aug 19, 2010 at 11:29 AM // reply »
3 Comments

Ben,

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

Mike


Aug 21, 2010 at 3:39 PM // reply »
11,238 Comments

@Mike,

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


Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools