Adobe ColdFusion 8

Left

Description

Returns the leftmost count characters in a string.

Returns

String; the first count characters in the string parameter.

Category

String functions

Function syntax

Left(string, count)

See also

Right, Mid, Len

Parameters

Parameter

Description

string

A string or a variable that contains one.

count

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

Example

<h3>Left Example</h3>

<cfif IsDefined("Form.myText")>
<!--- If len returns 0 (zero), then show error message. --->
    <cfif Len(Form.myText)>
        <cfif Len(Form.myText) LTE Form.RemoveChars>
            <cfoutput><p style="color: red; font-weight: bold">Your string #Form.myText# only has #Len(Form.myText)# characters. You cannot output 
    the #Form.removeChars# leftmost characters of this string because it is 
    not long enough.</p></cfoutput>
        <cfelse>
            <cfoutput><p>Your original string: <strong>#Form.myText#</strong></p>
                <p>Your changed string, showing only the <strong>#Form.removeChars#
                    </strong> leftmost characters:
                    <strong>#Left(Form.myText, Form.removeChars)#</strong></p>
            </cfoutput>
        </cfif>
    <cfelse>
        <p style="color: red; font-weight: bold">Please enter a string of more than 0 (zero) characters.</p>
</cfif>
</cfif>

<form action="<cfoutput>#CGI.ScriptName#</cfoutput>" method="POST">
<p>Type in some text<br />
<input type="Text" name="myText"></p>
<p>How many characters from the left do you want to show?
<select name="RemoveChars">
<option value="1">1
<option value="3" selected>3
<option value="5">5
<option value="7">7
<option value="9">9</select>
<input type="Submit" name="Submit" value="Remove characters"></p>
</form>