Skip to content

Instantly share code, notes, and snippets.

@s7github
Last active December 16, 2015 15:49
Show Gist options
  • Save s7github/5458450 to your computer and use it in GitHub Desktop.
Save s7github/5458450 to your computer and use it in GitHub Desktop.
"Common Coldfusion String Functions" _ MidLeft(string, start, num) - Returns n characters from the left side of starting position in the string
<cffunction name="MidLeft" returntype="string" output="no" hint="Returns numChars characters from the left side of startPos in the string">
<cfargument name="stringVal" type="string" required="yes" default="">
<cfargument name="startPos" type="numeric" required="yes" default="1">
<cfargument name="numChars" type="numeric" required="yes" default="0">
<!--- If startPos exceeds length of string argument then start picking characters from string end position --->
<cfif arguments.startPos gt len(arguments.stringVal)>
<cfset arguments.startPos = len(arguments.stringVal)>
</cfif>
<cfset var fromPos = arguments.startPos - arguments.numChars + 1>
<!--- Avoid 2nd parameter of mid function to goto 0 or below that --->
<cfif arguments.startPos - arguments.numChars + 1 lte 0>
<cfset fromPos = 1>
<cfset arguments.numChars = arguments.startPos>
</cfif>
<cfreturn mid(arguments.stringVal, fromPos, arguments.numChars)>
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment