left

Returns the leftmost count characters in a string.

left(String, count) → returns string

Member Function Syntax

string.left(count)

Argument Reference

String string
Required

count numeric
Required

An integer or a variable that contains one.
Number of characters to return.

Compatibility

ColdFusion:

The count can be a negative integer as of CF2018+

Examples
Sample code invoking the left function

In this example we'll use left() to return part of a string.

writeOutput( left( 'The quick brown fox jumped over the lazy dog', 19 ) );

Expected Result: The quick brown fox

CF 2018+ In this example we'll use a negative count to return part of a string. CF 2018+

writeOutput( left( 'The quick brown fox jumped over the lazy dog', -25 ) );

Expected Result: The quick brown fox

In this example we'll use left() in a function to help us to capitalize the first letter in a string.

public string function capitalize( required string text ) {
	return uCase( left( arguments.text, 1 ) ) & right( arguments.text, len( arguments.text ) - 1 );
}

In this example we'll use left() to test the first five characters of a request context variable.

if( listFindNoCase( 'super,great,coder,rulez', left( rc.answer, 5 ) ) ) {
	writeOutput( 'You are an awesome developer!' );
}

CF 11+ Lucee 4.5+ In this example we'll use left() as a member function inside a function to help us to capitalize the first letter in a string.

public string function capitalize( required string text ) {
	return arguments.text.left( 1 ).ucase() & arguments.text.right( arguments.text.len() - 1 );
}

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub