Created
May 31, 2015 10:42
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
*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