Skip to content

Instantly share code, notes, and snippets.

@lizlongnc
Last active August 29, 2015 14:07
Show Gist options
  • Save lizlongnc/1739ec4f663508a66a38 to your computer and use it in GitHub Desktop.
Save lizlongnc/1739ec4f663508a66a38 to your computer and use it in GitHub Desktop.
JS common string properties and methods
var str = 'my name is liz';
str.length // returns 14
charAt(index) - returns single char of a string at index postion
str.charAt(0); // returns "m"
str.charAt(3); // returns "n"
indexOf(string)
str.indexOf("n"); // returns 3
.split()
str.split(" "); //returns ["my", "name", "is", "liz"]
var strSplit = str.split(" "); //returns ["my", "name", "is", "liz"]
.slice()
str.slice(1,2); // returns "y" - note: 2nd arg - up to but not including
str.slice(1,5); // returns "y na"
.search() - uses regular expressions (regexp) - regular expressions use /pattern/flags; or new RegExp(pattern [, flags]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment