Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. /*
  2. STRING MANIPULATION
  3. The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string.
  4. Search() works the same way:
  5. */
  6.  
  7. var str = 'Find where the string starts';
  8. var res = str.indexOf('s');
  9. console.log(res); // prints 15, it means first 's' is at 15th character
  10.  
  11. /*
  12. The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
  13. */
  14.  
  15. var str = 'Find where the string starts';
  16. var res = str.lastIndexOf('s');
  17. console.log(res); // prints 27, we have a lot of 's' in a string but it returned the last one
  18.  
  19. /*
  20. If wanted index is not found on both ways, it will be returned -1.
  21. Slice() method extracts a part of a string and returns the extracted part in a new string.
  22. The method takes 2 parameters: the starting index (position), and the ending index (position).
  23. Substr() works similarly except it cannot take negative numbers.
  24. */
  25.  
  26. var str = 'Find where the string starts';
  27. var res = str.slice(0, 4);
  28. console.log(res); // prints 'Find' because I just took out a string between 0 and 4 indeces
  29.  
  30. /*
  31. Substr() is similar to slice().
  32. The difference is that the second parameter specifies the length of the extracted part.
  33. */
  34.  
  35. var str = 'Find where the string starts';
  36. var res = str.slice(0, 10);
  37. console.log(res); // prints 'Find where'
  38.  
  39. /*
  40. The replace() method replaces a specified value with another value in a string.
  41. This method can also take a regular expression as the search value.
  42. By default, the replace() function replaces only the first match.
  43. To replace all matches, use a regular expression with a g flag (for global match)
  44. */
  45.  
  46. var str = 'Find where the string starts';
  47. var res = str.replace('Find', 'Stop'); // replace 'Find' with 'Stop'
  48. console.log(res); // prints 'Stop where the string starts'
  49.  
  50. var str = 'Find the string and replace the string';
  51. var res = str.replace(/string/g, 'candy'); // using g flag
  52. console.log(res); // prints 'Find the candy and replace the candy'
  53.  
  54. /*
  55. We can convert to lower case or upper case using these methods: toLowerCase() and toUpperCase.
  56. */
  57.  
  58. var str = 'Find Where The String Starts';
  59. var res = str.toLowerCase();
  60. console.log(res); // prints 'find where the string starts'
  61.  
  62. var str = 'find where the string starts';
  63. var res = str.toUpperCase();
  64. console.log(res); // prints 'FIND WHERE THE STRING STARTS'
  65.  
  66. //Concat() joins two or more strings:
  67.  
  68. var str = 'Bye';
  69. var str2 = 'Bye!';
  70. var res = str.concat(" ", str2);
  71. console.log(res); // prints 'Bye Bye!'
  72.  
  73. /*
  74. CharAt() method returns the character at a specified index (position) in a string.
  75. CharCodeAt() method returns the unicode of the character at a specified index in a string.
  76. */
  77.  
  78. var str = 'String';
  79. var res = str.charAt(0); // asking for the first character
  80. console.log(res); // prints the first character 'S'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement