Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. /*
  2. * STRING MANIPULATION
  3. *
  4. * These are methods for manipulating and playing with strings!
  5. * ALL STRING METHODS RETURN A NEW STRING... Strings cannot be
  6. * changed, they can only be replaced.
  7. */
  8.  
  9. //string length//
  10. var string = "Here's some silly nonsense!!";
  11. console.log(string.length);
  12.  
  13. /*
  14. * similar to array.length, evaluates how many characters are
  15. * inside of a string.
  16. */
  17.  
  18. //searching strings//
  19. console.log(string.indexOf("s", 10));
  20. console.log(string.lastIndexOf("s"));
  21. console.log(string.search("s"));
  22.  
  23. /*
  24. * indexOf and search will return the first position of either
  25. * a string or character within a string, where as lastIndexOf
  26. * will return the last. You can define a starting position in
  27. * the indexOf method, as in the example above. The same CANNOT
  28. * be done with the search method. The search method, however,
  29. * can use regular expressions in it's parameters. So...
  30. *
  31. * string.search(/words/i);
  32. *
  33. * would perform a case insensitive search for the 'words'.
  34. * Other regular expressions include
  35. * g to perform a global search, it won't stop after the
  36. * first result.
  37. * m to perform multiline matching.
  38. *
  39. * There are other ways to utilize regular expressions in the
  40. * search method, for future reference..
  41. */
  42.  
  43. //extracting string parts//
  44. console.log(string.slice(16, 20));
  45.  
  46. /*
  47. * slice takes two arguments and returns the portion of the
  48. * string between those two indecies. Negative paramaters can
  49. * be used for both parameters. A negative in the first will
  50. * cause it to count from the end of the string, a negative in
  51. * the second will count backwards from the index defined in
  52. * the first. If you use this method with only the first
  53. * parameter, it will slice out the rest of the string.
  54. */
  55.  
  56. console.log(string.substring(16, 20));
  57.  
  58. /*
  59. * substring is nearly identical to slice, but it cannot take
  60. * negative numbers.
  61. */
  62.  
  63. console.log(string.substr(16, 4));
  64.  
  65. /*
  66. * substr differs from slice in that the second parameter
  67. * defines the length the string is extracted. It cannot be
  68. * negative. The first parameter functions identically to slice,
  69. * however.
  70. */
  71.  
  72. //replacing part of a string//
  73.  
  74. console.log(string.replace("nonsense", "very serious business"));
  75.  
  76. /*
  77. * The replace method takes two arguments and replaces the first
  78. * instance of the first argument with the second. By default, it
  79. * is case sensitive, so in my example NONsense would not replace
  80. * nonsense. Regular expressions can be used to expand it's reach.
  81. * /g is used to replace all matches (global matches). So..
  82. *
  83. * string.replace(/nonsense/g, "very serious business");
  84. *
  85. * /i can be used to make replace case insensitive.
  86. * string.replace(/NoNSEnse/i, "very serious business");
  87. *
  88. */
  89.  
  90. //toUpperCase and toLowerCase//
  91.  
  92. console.log(string.toUpperCase());
  93. console.log(string.toLowerCase());
  94.  
  95. /*
  96. * These methods return the string in uppercase or lowercase,
  97. * respectively.
  98. */
  99.  
  100. //joining strings//
  101.  
  102. var string2 = "But actually it's very serious.";
  103. console.log(string.concat(" ", string2));
  104.  
  105. /*
  106. * concat is used to join two or more strings. It works the same
  107. * as doing string1 + " " + string2;
  108. */
  109.  
  110. //extracting characters//
  111.  
  112. console.log(string.charAt(7));
  113. console.log(string.charCodeAt(7));
  114.  
  115. /*
  116. * These are, according to w3schools, the only two safe methods
  117. * for extracting a character from a string. charAt will evaluate
  118. * the character at the position, and charCodeAt evaluates the
  119. * unicode of the character at the specified index.
  120. */
  121.  
  122. console.log(string[7]);
  123.  
  124. /*
  125. * This method does not work with internet explorer 5, 6, or 7.
  126. * It makes your string look like an array which can be confusing
  127. * for others looking over your code. It's best to avoid for now.
  128. */
  129.  
  130. //convert string to array//
  131.  
  132. console.log(string.split(" "));
  133. console.log(string.split(""));
  134.  
  135. /*
  136. * The split method will create an array from a string at any
  137. * instances of the character passed into the argument. If you use
  138. * "" without a defined character, it splits the string at every
  139. * character. Neat!
  140. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement