ColdFusion string functions let you measure, search, compare, split, and change text in CFML. Common choices include Len, Find, Mid, Replace, Trim, and ListToArray; the right function depends on whether you are treating a value as plain text, a delimited list, or a pattern.
What ColdFusion string functions do
In CFML, a string is text held in a variable or returned by an expression. String functions operate on that text and return a result, so a call usually does not alter the original variable unless you assign the returned value back to it. For example, cleanName = Trim(rawName) stores a trimmed result in a new variable. Function names are commonly written in camel case, but CFML function names are case-insensitive.
ColdFusion includes functions for everyday text work as well as functions that are useful for specific data formats. Some functions work with literal characters, while regular-expression functions interpret a pattern. List functions treat delimiters as boundaries between items. Knowing which kind of operation you need prevents subtle mistakes, such as searching for a literal dot with a regular expression or assuming a comma-separated value is automatically a safe data format.
These examples use CFScript syntax. CFML tags can also call functions inside expressions, and the underlying function behavior is generally the same. Exact behavior can vary in edge cases between Adobe ColdFusion and Lucee, and between versions, so check the documentation for the engine and version deployed by your application.
Length, positions, and extracting text
Start with Len when you need the length of a string. For example, Len("ColdFusion") returns 10. Left and Right return a specified number of characters from the beginning or end. With Left("ColdFusion", 5), the result is "Cold"; Right("ColdFusion", 6) returns "Fusion".
Mid extracts a portion using a starting position and a count. ColdFusion string positions are one-based, so the first character is position 1, not 0. Mid("ColdFusion", 5, 6) returns "Fusion". If your positions come from another language or an API that counts from zero, convert them before using them with CFML string functions.
Find locates a substring and returns its position, or 0 when it is not found. FindNoCase performs a case-insensitive search. For example, FindNoCase("fusion", "ColdFusion") returns 5. Both functions can take an optional starting position, which is useful when searching repeatedly through a string. A common bug is treating a not-found result as a valid character position; test for a result greater than zero before passing it to Mid or another position-based function.
Changing and comparing text
Use Trim to remove whitespace at the beginning and end of a value, and LTrim or RTrim when only one side should be cleaned. Trimming is useful for user input and imported data, but it does not remove spaces between words. For example, trimming " Ada Lovelace " yields "Ada Lovelace"; it does not turn the name into "AdaLovelace".
UCase and LCase change letter case. Replace substitutes matching literal text, while ReplaceNoCase searches without regard to letter case. Specify the scope when you want every match replaced: Replace("red red", "red", "blue", "all") returns "blue blue". Without a deliberate scope, code may replace fewer occurrences than intended. These functions perform literal replacement; they do not interpret the search text as a regular expression.
Compare and CompareNoCase compare two strings and return a number indicating their ordering: zero means equal, while a negative or positive result indicates which string sorts before the other. Use CompareNoCase when differences in letter case should not affect the comparison. These functions are not substitutes for validating that an input belongs to an allowed set, and sorting behavior can be affected by language and character details. For fixed options, explicit equality checks or a controlled lookup may be clearer.
Searching with regular expressions
When a search depends on a pattern rather than a fixed sequence of characters, use a regular-expression function. ReFind searches for a pattern, and ReReplace replaces text that matches one. Their case-insensitive counterparts include ReFindNoCase and ReReplaceNoCase. For example, ReReplace("Order 42", "[0-9]+", "NUMBER", "all") returns "Order NUMBER": [0-9]+ matches one or more digits.
Regular expressions have their own syntax. A period normally means “any character,” so a pattern intended to find a literal period must escape it, such as \.. Backslashes may also need care when a pattern is written inside a string literal. Keep patterns simple when possible, name complicated patterns clearly, and test them against both expected matches and non-matches. The regex engine and supported details can differ across runtime versions, so do not assume every feature from another programming language is available unchanged.
Do not use a regular expression merely because it seems more powerful. For a fixed word or phrase, Find or Replace is easier to read and less likely to misinterpret punctuation. Conversely, literal replacement is not suitable for rules such as replacing every sequence of digits or collapsing repeated whitespace; those are pattern-based tasks.
Lists, delimiters, and splitting strings
CFML list functions handle simple strings whose items are separated by delimiters. Common functions include ListLen, ListGetAt, ListFind, ListAppend, and ListToArray. For example, ListGetAt("red,green,blue", 2) returns "green". List positions are also one-based. Delimiters are treated as individual separator characters, not as a quoted CSV grammar with escaping rules.
ListToArray is useful when you want to work with items as an array rather than repeatedly retrieving list positions. For instance, a simple comma-delimited setting such as "small,medium,large" can be converted to an array and looped over. Be aware that empty items and consecutive delimiters need deliberate handling: list functions have options related to including empty fields, and defaults or details should be checked for the specific function and engine you use.
A comma-separated string is not automatically a reliable way to store arbitrary user data. If an item itself can contain a comma, or if fields may contain quotes and line breaks, a basic list operation may split it incorrectly. Use a proper CSV parser for CSV files, or a structured format such as JSON where appropriate. List functions are best for uncomplicated delimiter-based values whose rules are known.
How to choose the right function
Choose a function by first identifying what the input represents and what result you need. A direct, named function is usually more maintainable than building the same behavior from several smaller operations.
- To count characters, use
Len; to take text from an edge, useLeftorRight. - To extract text at a known position, use
Mid; to locate a literal substring, useFindorFindNoCase. - To replace a fixed phrase, use
ReplaceorReplaceNoCase; to match a pattern, considerReReplace. - To clean outer whitespace, use
Trim; to process simple delimiter-separated items, use list functions. - To compare values while ignoring case, use
CompareNoCaseor an explicit case-normalization approach that fits the requirement.
For example, suppose a form submits a username with accidental spaces and you need to look for it without case sensitivity. Trim the input first, then use a case-insensitive comparison or database query appropriate to your application. Do not assume that changing the displayed case is the same as safely validating the value, or that a string function can replace parameterized database queries.
Common errors and important safety distinctions
One frequent source of errors is confusing a string function’s return value with a change to the original variable. Assign the result when you want to keep it: message = Replace(message, "old", "new", "all"). Another is mixing up a zero-based position from a different language with CFML’s one-based positions, or forgetting that Find returns 0 when there is no match.
Do not confuse escaping or encoding with string cleanup. Removing characters from a value does not make it safe for HTML, JavaScript, a URL, or SQL. Encode output for its destination; for HTML output, for example, use an appropriate HTML encoding function such as EncodeForHTML where supported by the ColdFusion version in use. Use parameterized queries for SQL rather than trying to sanitize SQL input with Replace. Security depends on the output or input context, not merely on whether a string looks tidy.
Also consider nulls, empty strings, and non-ASCII text. Whether a null value is accepted, converted, or raises an error can depend on the engine, version, and null-support configuration. Test those cases explicitly rather than relying on assumptions. Most ordinary text works as expected, but characters outside basic English letters can reveal differences in case conversion, sorting, or indexing. If exact character counts matter for international text, test representative characters on the production runtime.
Frequently Asked Questions
Are ColdFusion string positions zero-based?
No. Functions such as Mid and Find use one-based positions, so the first character is at position 1. A search that finds nothing returns 0.
What is the difference between Replace and ReReplace?Replace searches for literal text. ReReplace interprets its search argument as a regular-expression pattern, which is useful for variable patterns but requires regex syntax.
How do I replace every occurrence of a word?
Use Replace with the scope set to "all", or use ReplaceNoCase if matching should ignore case. Check the function’s argument order for the runtime version you target.
Does Trim remove spaces inside a string?
No. Trim removes whitespace at the two ends. It leaves internal spaces in place, so " one two " becomes "one two".
Should I use list functions to parse CSV?
Not for general CSV data. List functions are designed for simple delimiter-separated values and do not provide full CSV quoting and escaping behavior. Use a CSV parser when fields may contain delimiters, quotes, or line breaks.
Practical takeaway
ColdFusion provides a focused toolkit for text: measurement and extraction functions, literal searches and replacements, case and comparison helpers, regular-expression tools, and list operations. Prefer the simplest function that matches the data format, make positions and replacement scope explicit, and test edge cases such as missing matches, empty values, delimiters, and international characters. Keep text transformation separate from output encoding and database safety; those are different responsibilities, even when they all involve strings.
Related
- How to Split an Activity in P6: Step-by-Step Guide
- JavaScript String Operations: Methods and Examples
- SQL Split Part: Extract a Value by Position
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Excel Sum Whole Column: Formulas, Examples, and TipsTo sum every numeric value in an Excel column, enter =SUM(A:A) in a cell outside column A and…
- MATLAB split Function: Syntax and ExamplesMATLAB’s split function separates text into pieces at a delimiter and returns the pieces as a string array.…
- Excel Left Trim Formula: Remove Characters from StartThe quickest way to left-trim (remove characters from the start of) a text string in Excel is with…
- Add in Excel Without Hidden Rows: Use the SUBTOTAL FormulaTo add only the visible rows in an Excel range, use the SUBTOTAL function with function number 109.…
- Excel MATCH Arguments: What Each One DoesExcel’s MATCH function has three arguments: MATCH(lookup_value, lookup_array, [match_type]). The first two say what to look for and…
- Filter Strikethrough in Excel with a Helper ColumnTo filter rows by strikethrough in Excel, add a helper column that checks the formatting of the cell…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.