View comments | RSS feed

Mid

Description

Extracts a substring from a string.

Return value

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

Category

String functions

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-quote or double-quote delimited.
start
A positive integer or a variable that contains one. Position at which to start count.
count
A positive integer or a variable that contains one. Number of characters to return. (0 is not valid, but it does not throw an error.)

Example

<h3>Mid Example</h3>
<cfif IsDefined("Form.MyText")>
<!--- if len is 0, then err --->
  <cfif Len(FORM.myText) is not 0>
    <cfif Len(FORM.myText) LTE FORM.RemoveChars>
    <p>Your string <cfoutput>#FORM.myText#</cFOUTPUT> only has
<cfoutput>#Len(FORM.myText)#</cfoutput> characters. You cannot output
the <cfoutput>#FORM.removeChars# </cfoutput> middle characters of this
string because it is not long enough
    <cfelse>
    
    <p>Your original string: <cfoutput>#FORM.myText#</cfoutput>
    <p>Your changed string, showing only the <cfoutput>#FORM.removeChars#
</cfoutput> middle characters:
    <cfoutput>#Mid(Form.myText, FORM.removeChars, Form.countChars)#</cfoutput>
    </cfif>

ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


No screen name said on Mar 9, 2006 at 10:34 PM :
It should be noted that you're only removing FORM.removeChars - 1 characters from the beginning of the string. Also, you should use lt instead of lte when checking the string length, otherwise there is no way to output just the last character.

These don't really matter for a hypothetical example (unless you want to run it), but you're not closing the first two <cfif> tags or any of your <p> tags and the text doesn't really match what the program is doing.

This would be a more useful example:
<cfset _str = "this">
<cfset _start = 4>
<cfset _len = 1>
<cfoutput>
<cfif Len(_str) lt (_start + _len - 1)>
<p>Your string #_str# only has #Len(_str)# characters. You cannot output
#_len# characters of this string starting at character number #_start# because it
is not long enough</p>
<cfelse>
<p>Your original string: #_str#</p>
<p>Your changed string, showing only #_len# character(s), starting at
character number #_start#: #Mid(_str, _start, _len)#</p>
</cfif>
</cfoutput>

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt241.htm