View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > Mid PreviousNext

Mid

Extracts a substring from a string.

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

String functions

Mid(string, start, count)

Left, Len, Right

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. Positions start with 1, not 0.

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.)

<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>

Contents > CFML Reference > ColdFusion Functions > Mid PreviousNext

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

Version 6.1

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

Comments


Sarge said on Mar 20, 2004 at 9:23 AM :
We are working to improve the examples in the ColdFusion reference pages. We propose to replace the current example on this page with the the following example. If you have any comments on this example, add them to this page.

This example shows how to use the mid function to return a substring of characters.

<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>
TWesson said on May 6, 2004 at 1:07 AM :
Regarding the MID () function, the last parameter should be optional.

Therefore, MID ( strTest, 5 ) would return the 5th and remaining characters of strTest.
dspent said on Dec 27, 2004 at 12:50 PM :
this is about the examples on ALL of the pages...

the example code on most if not all of the CFMX 6.1 pages are poorly documented for those users (like me) who are just learning.... For instance, you guys will introduce a tag or function etc...and explain what its supposed to do, then jump right into this extravagant example code without any real documentation of what's going on in the code and without any screenshots or explanations of what the user or coder should expect to see in terms of output.....

This should be high priority to make the livedocs more user friendly and "USEABLE!!!"
jrunrandy said on Jan 6, 2005 at 9:43 AM :
Given that you are just learning, have you tried the Getting Started manual, available in Livedocs at http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/part_get.htm ?

Also, you will find additional usage information in the Developing Applications book, available in LiveDocs at http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/part_dev.htm

You might find that the PDF-format docs are easier to read, though: http://www.macromedia.com/support/documentation/en/coldfusion/
Tacs said on Jun 29, 2005 at 7:40 AM :
Even for people who are not learning, you should give example output in the comments of the code. Also the code should be color coded. Here is an example:

http://us4.php.net/manual/en/function.substr.php
mrhapyhipy said on Aug 2, 2005 at 4:02 AM :
As TWesson said on May 6, 2004 at 1:07 AM
The third parameter should be optional.

Please put it in as an enhansment.

 

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

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/functa42.htm