MidString(string, from[, to])
Last updated November 02, 2001
Version: 1 | Requires: CF5 | Library: StrLib
Description:
The Midstring function returns the middle string between two substrings. The from and to strings should be unique in the string from which you want the substring because the function only looks for the first match of each string. So if you have string "ApplePearApple" and enter "Apple" as both the to and the from string, you will get an empty string back.
Note that all strings are case sensitive.
Return Values:
Returns the string between the delimiters.
Example:
<cfset string="ApplePearPlum">
<cfset fromstr="Apple">
<cfset tostr="Plum">
<cfset result = midstring(string,fromstr,tostr)>
<cfoutput>
string=#string#<BR>
Mid from Apple to Plum = #result#<BR>
</cfoutput>
<cfset string="ApplePearPlumApple">
<cfset fromstr="Apple">
<cfset result = midstring(string,fromstr)>
<cfoutput>
string=#string#<BR>
Mid using only Apple = #result#
</cfoutput>
Parameters:
Name | Description | Required |
---|---|---|
string | The string to check. | Yes |
from | The initial string to use as a delimiter. | Yes |
to | The ending string to use as a delimiter. | No |
Full UDF Source:
/**
* Midstring: Return the middle part of a string between a specified start substring and a specified end substring.
*
* @param string The string to check.
* @param from The initial string to use as a delimiter.
* @param to The ending string to use as a delimiter.
* @return Returns the string between the delimiters.
* @author Andrew Cripps (andrew@cripps.net)
* @version 1, December 3, 2001
*/
function midstring(string,from) {
var start="";
var end="";
var lenstart="";
var to=from;
if(arrayLen(arguments) gte 3) to = arguments[3];
start = refind(from,string);
end = refind(to,string,len(from)+start);
lenstart = len(from);
return mid(string,start+lenstart,max(end-start-lenstart,0));
}
Search CFLib.org
Latest Additions
Raymond Camden added
QueryDeleteRows
November 04, 2017
Leigh added
nullPad
May 11, 2016
Raymond Camden added
stripHTML
May 10, 2016
Kevin Cotton added
date2ExcelDate
May 05, 2016
Raymond Camden added
CapFirst
April 25, 2016