Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. <script id="jsbin-javascript">
  2. /*
  3. * Strings: Character Data
  4. *
  5. * Strings create words, phrases, etc
  6. * (a string of characters)
  7. */
  8.  
  9. var myString = "Hello!"
  10.  
  11. // to access the specific characters in a string:
  12. // value: H e l l o
  13. // index: 0 1 2 3 4
  14.  
  15. // use brackets [] -> aString[index]
  16. var h = myString[0];
  17. console.log(h);
  18.  
  19. /** OR **/
  20.  
  21. // use the method charAt() -> aString.charAt(index)
  22. var e = myString.charAt(1);
  23. console.log(e);
  24.  
  25. /** OR **/
  26.  
  27. // use the method slice() -> aString.slice(index, index)
  28. var l = myString.slice(2,3); // starts at index 2 and ends before 3
  29. console.log(l);
  30.  
  31. var loEx = myString.slice(3); // starts at index 3 and goes until the end of the string
  32. console.log(loEx);
  33.  
  34.  
  35. // note: can access the last character of a string by using .length
  36. var loEx = myString.charAt(aString.length-1);
  37. console.log(loEx);
  38. </script>
  39.  
  40.  
  41. <script id="jsbin-source-javascript" type="text/javascript">/*
  42. * Strings: Character Data
  43. *
  44. * Strings create words, phrases, etc
  45. * (a string of characters)
  46. */
  47.  
  48. var myString = "Hello!"
  49.  
  50. // to access the specific characters in a string:
  51. // value: H e l l o
  52. // index: 0 1 2 3 4
  53.  
  54. // use brackets [] -> aString[index]
  55. var h = myString[0];
  56. console.log(h);
  57.  
  58. /** OR **/
  59.  
  60. // use the method charAt() -> aString.charAt(index)
  61. var e = myString.charAt(1);
  62. console.log(e);
  63.  
  64. /** OR **/
  65.  
  66. // use the method slice() -> aString.slice(index, index)
  67. var l = myString.slice(2,3); // starts at index 2 and ends before 3
  68. console.log(l);
  69.  
  70. var loEx = myString.slice(3); // starts at index 3 and goes until the end of the string
  71. console.log(loEx);
  72.  
  73.  
  74. // note: can access the last character of a string by using .length
  75. var loEx = myString.charAt(aString.length-1);
  76. console.log(loEx);</script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement