Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- String Manipulation
- Strings are manipulated by using built-in methods.
- Normally methods are not available to primitive values, but Javascript treats strings as arrays, which have a multitude of built-in methods.
- ------
- .length
- */
- var name = "Jennifer";
- console.log(name.length); // prints 8 the character length of string "Jennifer"
- /*
- .indexOf() & .lastIndexOf()
- */
- console.log(name.indexOf("n")); //prints 2, the position of the first instance of given input
- console.log(name.lastIndexOf("n")); //prints 3, the position of the last instance of given input
- /*
- /*
- .slice()
- */
- console.log(name.slice(2)); // prints "nnifer", if one input, accesses characters remaining after given input array position
- console.log(name.slice(0,3)); // prints "Jen", accesses characters including starting and between ending positions - accessed by 2 inputs
- /*
- .replace()
- */
- console.log(name.replace("nifer", " and Brad")); // prints "JenandBrad", replaces first input with second input
- /*
- toUpperCase() & toLowerCase()
- */
- console.log(name[0] + name.slice(1,name.length).toUpperCase()); // prints "JENNIFER", Adds first (already capitalized)letter to sliced remainder of string which is then capitalized.
- console.log(name.toLowerCase()); //prints "jennifer", uncapitalized any and all capital letters in string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement