DevLearningTools

🚧 This site is under active construction — new tools, guides, and pages are added every week.

MODULE 2 · LESSON 06

Strings

Creating, joining, searching, and modifying text in ColdFusion — len(), trim(), ucase(), lcase(), replace(), find(), mid(), left(), right(), and listToArray(), in both CFScript and Tag Syntax.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

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
<cfscript>

name = "John";
city = "Delhi";
course = "ColdFusion";

</cfscript>
Tag Syntax
<cfset name = "John">
<cfset city = "Delhi">
<cfset course = "ColdFusion">

Displaying Strings

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Concatenating Strings

Use the & operator to join strings.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

String Length

Use len() to count characters.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Convert to Uppercase

Use ucase().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Convert to Lowercase

Use lcase().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Remove Extra Spaces

Use trim().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Replace Text

Use replace().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Find Text

Use find().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

find() returns the starting position of the text. If it isn't found, it returns 0.

Extract Part of a String

Use mid().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Left and Right

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Split a String

Use listToArray() to turn a delimited list into an array.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Check if a String Contains Text

Use findNoCase() for case-insensitive searches.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Real-World Example

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Common String Functions

FunctionPurpose
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.