User Guide
776 Chapter 3: ColdFusion Functions
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
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>
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.)