ColdFusion Insert() - How Did I Miss This Function?
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 :)
Want to use code from this post? Check out the license.
I KNEW I'd seen this post before!
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.