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

Posted July 16, 2007 at 5:35 PM

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:

 Launch code in new window » Download code as text file »

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

Download Code Snippet ZIP File

Comments (3)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




ColdFusion Jobs - Find or Post A ColdFusion Job Through DeveloperCircuit.com

Reader Comments

heh...

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

:)

Posted by charlie griefer on Jul 16, 2007 at 6:31 PM


Ha Charlie!

I KNEW I'd seen this post before!

Posted by Michael Sharman on Jul 16, 2007 at 6:39 PM


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

Posted by Ben Nadel on Jul 16, 2007 at 8:05 PM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting