Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. /*
  2. String Manipulation
  3.  
  4. Strings are manipulated by using built-in methods.
  5.  
  6. Normally methods are not available to primitive values, but Javascript treats strings as arrays, which have a multitude of built-in methods.
  7.  
  8. ------
  9.  
  10. .length
  11.  
  12. */
  13.  
  14.  
  15. var name = "Jennifer";
  16.  
  17. console.log(name.length); // prints 8 the character length of string "Jennifer"
  18.  
  19. /*
  20.  
  21. .indexOf() & .lastIndexOf()
  22. */
  23.  
  24. console.log(name.indexOf("n")); //prints 2, the position of the first instance of given input
  25.  
  26. console.log(name.lastIndexOf("n")); //prints 3, the position of the last instance of given input
  27.  
  28. /*
  29.  
  30. /*
  31.  
  32. .slice()
  33. */
  34.  
  35. console.log(name.slice(2)); // prints "nnifer", if one input, accesses characters remaining after given input array position
  36.  
  37. console.log(name.slice(0,3)); // prints "Jen", accesses characters including starting and between ending positions - accessed by 2 inputs
  38.  
  39. /*
  40.  
  41. .replace()
  42. */
  43.  
  44. console.log(name.replace("nifer", " and Brad")); // prints "JenandBrad", replaces first input with second input
  45.  
  46. /*
  47.  
  48. toUpperCase() & toLowerCase()
  49. */
  50.  
  51. 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.
  52.  
  53. console.log(name.toLowerCase()); //prints "jennifer", uncapitalized any and all capital letters in string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement