Skip to content

Instantly share code, notes, and snippets.

@designviacode
Created May 31, 2015 10:42
Show Gist options
  • Save designviacode/38b37ee4010ddd49ddd0 to your computer and use it in GitHub Desktop.
Save designviacode/38b37ee4010ddd49ddd0 to your computer and use it in GitHub Desktop.
Find character Locations: first-char, last-char, any Index position , index Number in String
/*
*Below Function can find character Locations:
* firstCharacter, lastCharacter, at any Index position
* index Numbers in String
*/
String.prototype.charLocation = function(pos) {
if(pos === 'first' || pos === "f" || pos === "F") {
return this.charAt(0);
}
else if(pos === "last" || pos === "l" || pos === "L") {
return this.slice(-1);
}
else if(typeof(parseInt(pos)) === "number") {
return this.charAt(pos);
}
};
var myName = "Manpreet";
//Returns character at last String of above variable
console.log(myName.charLocation("fl"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment