Almost every ColdFusion application works with text. Whether you're displaying a user's name, validating an email address, generating URLs, or formatting messages, you're working with strings.
A string is simply a sequence of characters enclosed in quotation marks — examples include "Hello", "ColdFusion", "john@example.com", and "Welcome John".
ColdFusion provides many built-in functions for creating, searching, modifying, and formatting strings, making text manipulation simple and powerful. By the end of this lesson, you'll know how to create strings, join them together, find text, replace text, change letter case, remove extra spaces, split strings into arrays, and use many of ColdFusion's most commonly used string functions.
Learning Objectives
After completing this lesson, you'll be able to:
- Understand what a string is.
- Create string variables.
- Concatenate strings.
- Find the length of a string.
- Convert text to uppercase and lowercase.
- Search and replace text.
- Remove extra spaces.
- Extract part of a string.
- Split strings into arrays.
- Use common ColdFusion string functions.
What Is a String?
A string stores text — names, addresses, email IDs, phone numbers, messages, URLs, and so on.
<cfscript> name = "John"; city = "Delhi"; course = "ColdFusion"; </cfscript>
<cfset name = "John"> <cfset city = "Delhi"> <cfset course = "ColdFusion">
Displaying Strings
Concatenating Strings
Use the & operator to join strings.
String Length
Use len() to count characters.
Convert to Uppercase
Use ucase().
Convert to Lowercase
Use lcase().
Remove Extra Spaces
Use trim().
Replace Text
Use replace().
Find Text
Use find().
find() returns the starting position of the text. If it isn't found, it returns 0.
Extract Part of a String
Use mid().
Left and Right
Split a String
Use listToArray() to turn a delimited list into an array.
Check if a String Contains Text
Use findNoCase() for case-insensitive searches.
Real-World Example
Common String Functions
| Function | Purpose |
|---|---|
| len() | Length of string |
| trim() | Remove spaces |
| ucase() | Uppercase |
| lcase() | Lowercase |
| replace() | Replace text |
| replaceNoCase() | Case-insensitive replace |
| find() | Find text |
| findNoCase() | Case-insensitive search |
| left() | Left characters |
| right() | Right characters |
| mid() | Middle part |
| reverse() | Reverse text |
| repeatString() | Repeat string |
| listToArray() | Convert list to array |
Common Beginner Mistakes
Forgetting Quotes
name = John; treats John as a variable name, not text, and throws an error. Write name = "John"; instead.
Using + Instead of &
firstName + lastName tries to add the strings numerically. Use firstName & lastName to join text together.
Forgetting trim()
Extra leading or trailing spaces from user input often cause validation problems later — for example "john@example.com " failing an exact-match check. Run trim(name) before saving or comparing user input.
Best Practices
- Use meaningful variable names.
- Trim user input before processing.
- Use findNoCase() for user searches.
- Prefer built-in string functions over manual logic.
- Keep user input validated before storing it.
Interview Questions
What is a string?
A string is a sequence of characters enclosed in quotation marks.
Which operator joins two strings?
The & operator.
Which function returns the string length?
len()
How do you convert text to uppercase?
ucase()
Which function removes leading and trailing spaces?
trim()
How do you search without considering case?
findNoCase()
Summary
In this lesson, you learned how to create, display, join, search, and modify strings in ColdFusion using both CFScript and Tag Syntax. You explored commonly used string functions such as len(), trim(), ucase(), lcase(), replace(), find(), mid(), and listToArray(). These functions are used in almost every ColdFusion application, from processing user input to generating dynamic web pages.
What's Next?
The next lesson covers Numbers — working with integers and decimals, arithmetic operations, rounding, formatting numbers, generating random numbers, and using ColdFusion's built-in numeric functions in both CFScript and Tag Syntax.