Adobe ColdFusion 8

Mid

Description

Extracts a substring from a string.

Returns

A string; the set of characters from string, beginning at start, of length count.

Category

String functions

Function syntax

Mid(string, start, count)

See also

Left, Len, Right

Parameters

Parameter

Description

string

A string or a variable that contains one. Must be single-quotation mark or double-quotation mark delimited.

start

A positive integer or a variable that contains one. Position at which to start count. Positions start with 1, not 0.

count

A positive integer or a variable that contains one. Number of characters to return. (Zero is not valid, but it does not throw an error.)

Example

<h3>Mid 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# middle characters of this string because it is
            not long enough.</p></cfoutput>
        <cfelseif Form.startPos GTE Len(Form.myText)>
            <cfoutput><p style="color: red; font-weight: bold">Your string
            #Form.myText# only has #Len(Form.myText)# characters. You cannot start
            at position #Form.startPos#.</p></cfoutput>
        <cfelse>
            <cfoutput><p>Your original string: <strong>#Form.myText#</strong></p>
            <p>Your changed string, showing only the <strong>#Form.removeChars#
            </strong> middle characters: <strong>#Mid(Form.myText, 
            Form.startPos, 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>Enter a starting position (from the beginning of the entered text)<br />
<input name="startPos" type="text" size="1"></p>
<p>How many characters 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>