Skip to content

Instantly share code, notes, and snippets.

@ivanionut
Forked from s7github/MidLeft.cfm
Created May 29, 2014 07:29
Show Gist options
  • Save ivanionut/199a2e1bebaede1fb160 to your computer and use it in GitHub Desktop.
Save ivanionut/199a2e1bebaede1fb160 to your computer and use it in GitHub Desktop.
<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