Monday, July 20, 2026

ColdFusion String Functions - The 5 Minute Mission

You don't have hours to spend ramping up on ColdFusion.
You need to get in. Get out and deliver. Let's do that.

Copy and paste this into a cffiddle.org session.
Yep. Remember to wrap it with <cfscript> tags. Feel free to thank me in the comments.

// ---------------------------------------------------------------------------------------
// Tutorials with a SMART Mission
// What does that look like?
// 
// SMART: Specific, Measurable, Achievable, Revelant and Time-bound
// S: String Functions
// M: Read and digest within 5 minutes
// A: Anyone new/familiar with programing can use them immediately
// R: Use a data in a common ways anyone will mentally and emotionally connnect with
// T: Read within 3 min, Research more within seconds and Experiment with immediately.
// ---------------------------------------------------------------------------------------

// ColdFusion strings are variables made up of characters (letters, numbers, etc.)

// 1. Define the initial data sets
fruitList  = "Apples,Bananas,Blueberries,Strawberries,Grapes,Oranges,Cantaloupe,Watermelon,Kiwi,Pomegranate";
veggieList = "Spinach,Kale,Broccoli,Cauliflower,Radish,Carrot,Potato,Pepper,Corn,Onion";
meatList   = "Chicken,Turkey,Fish,Beef,Pork";

missionIntroduction = "
    <p><b>ColdFusion String Functions</b><br>
    As a CF developer, you use these often. Pick these up. You gonna need all 11 of 'em.</p>

    <p><b>MISSION:</b> Operation Groceries. Go help your Mama because she's a remarkable lady.</p>

    <p><b>VITAL DATA</b><br>
    <b>Fruits</b> (#listLen(fruitList)#): #fruitList#<br>
    <b>Veggies</b> (#listLen(veggieList)#): #veggieList#<br>
    <b>Meat</b> (#listLen(meatList)#): #meatList#</p>

    <p><b>NEED TO KNOW</b><br><b>1-Based Indexing:</b> ColdFusion strings start at index 1.<br>
    Yes, they start at 1. What the heck? It's ColdFusion's thing.<br>
    Call it a quirk. Get used to it. Love the number 1 and like it.<br>
    Other languages have their indexes start at 0 like JavaScript, C++, Java or Python.</p>
";
writeOutput(missionIntroduction);

writeOutput("<b>EXAMPLES</b><br>");

// 1. Execute String Length
lenResult = Len(meatList); 
writeOutput("1. Len(meatList) - #lenResult#  (that's characters to you, soldier)<br><br>");

// 2. Trim string of leading and trailing spaces
paddedMeat  = "  " & meatList & "  ";
writeOutput("<em>paddedMeat has spaces:</em> '#paddedMeat#'<br>");
trimResult  = Trim(paddedMeat); 
writeOutput("2.Trim(paddedMeat) - '#trimResult#' (trimmed leading/trailing spaces)<br><br>");

// 3. Execute conversion to Uppercase and Lowercase
ucResult = UCase(meatList);
writeOutput("3a. UCase(meatList) - #ucResult#<br>");

lcResult = LCase(veggieList); 
writeOutput("3b. LCase(veggieList) - #lcResult#<br><br>");

// 4. Execute extraction of substrings
leftResult = Left(fruitList, 6); // Apple 
writeOutput("4a. Left(fruitList, 6) - #leftResult#<br>");

rightResult = Right(veggieList, 5); // Onion
writeOutput("4b. Right(veggieList, 5) - #rightResult#<br>");

midResult = Mid(meatList, 9, 6);  // Turkey
writeOutput("4c. Mid(meatList, 9, 6) - #midResult#<br><br>");


// 5. Execute Finding and Searching (1-based index results. Found = index, Not Found = 0)
// Case-sensitive search...
findFailResult = Find("beef", meatList); 
writeOutput("5a. Find('beef') - #findFailResult# (not found; case-sensitive search)<br>");

findResult = Find("Beef", meatList);
writeOutput("5b. Find('Beef') - #findResult# (found at index 21; case-sensitive search) <br>");

// Non-case-sensitive search...
findNoCaseResult = FindNoCase("beef", meatList);
// IMHO: Everyone should always capitalize Beef anyway, because it's the best meat on the planet.
writeOutput("5c. FindNoCase('beef') - #findNoCaseResult# (found at index 21 - NOT case-sensitive) <br><br>");

// 6. Execute String Modifications (Insert uses 0-based index)
replaceResult = Replace(fruitList, "Strawberries", "Blackberries"); 
writeOutput("6a. Replace(fruitList, 'Strawberries', 'Blackberries')<br> #replaceResult#<br>");
insertResult = Insert("Lamb,", meatList, 8); 
writeOutput("6b. Insert('Lamb,' meatList, 8) - #insertResult#<br>");

// Include any additional information that helps summarizes the above examples
additionalPoints = "<br><hr>
<p>
 <strong>Additional Points:</strong>
 <ul>
  <li><b>Strings Under the Hood:</b> Long strings can impact on performance.  Keep 'em short.  Too long? Then look into Arrays.</li>
  <li><b>Insert, but no Remove:</b> Correct.  If you want remove a chunk of a string you would use replace.</li>
  <li><b>Missed GetToken:</b> Good catch!  I did that on purpose.  That will be discussed in the next post on Arrays.</li>
 </ul>
 </p>";
writeOutput(additionalPoints);

MORE INPUT
After you've bench pressed this code, go have a look at ColdFusion documentation at Adobe and Foundeo's CFDocs. Then read about the string performance article by Brad Wood Confessions of a Speed Junky: How I made my code faster.

No comments: