Left
Extract the leftmost count characters from a string
Method Signature
Left(string=[string], count=[integer])
Arguments
Argument
Type
Required
Description
Default
string
string
true
The string to extract from
count
integer
true
The number of characters to retrieve.
Examples
Using left() on a string
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 ) );
Result: The quick brown fox
Using left() with a negative count on a string
In this example we'll use a negative count to return part of a string.
writeOutput( left( "The quick brown fox jumped over the lazy dog", -25 ) );
Result: The quick brown fox
Using left() in a function
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 );
}
Using left() to test values
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!" );
}
Using left() as a member function
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 );
}
Additional Examples
dump( left( "abcd", 3 ) ); // abc
dump( left( "Peter", -1 ) );
// Pete
Related
Last updated
Was this helpful?