Skip to content

Instantly share code, notes, and snippets.

@dmelcer9
Created May 7, 2015 14:08
Show Gist options
  • Save dmelcer9/e48738b08fd784e4ca67 to your computer and use it in GitHub Desktop.
Save dmelcer9/e48738b08fd784e4ca67 to your computer and use it in GitHub Desktop.
Simple substring implementation
//Simple substr implementation
//Paramaters
//String:the string you want to operate on
//Start:starting position, indexed from 0
//End:ending position, indexed from 0
//Curpos:used internally by the recursion loop- leave blank
//Examples
//echo(substr("Hello world",0,4)); Returns "Hello"
//echo(substr("Hello world",6,10)); Returns "world"
//If start/end are out of range from the string, those values will show up as undef in the resulting string
function substr(string,start,end,curpos=0)=(start+curpos < end)?str(string[start+curpos],substr(string,start,end,curpos+1)):string[start+curpos];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment