Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- STRING MANIPULATION
- The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string.
- Search() works the same way:
- */
- var str = 'Find where the string starts';
- var res = str.indexOf('s');
- console.log(res); // prints 15, it means first 's' is at 15th character
- /*
- The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
- */
- var str = 'Find where the string starts';
- var res = str.lastIndexOf('s');
- console.log(res); // prints 27, we have a lot of 's' in a string but it returned the last one
- /*
- If wanted index is not found on both ways, it will be returned -1.
- Slice() method extracts a part of a string and returns the extracted part in a new string.
- The method takes 2 parameters: the starting index (position), and the ending index (position).
- Substr() works similarly except it cannot take negative numbers.
- */
- var str = 'Find where the string starts';
- var res = str.slice(0, 4);
- console.log(res); // prints 'Find' because I just took out a string between 0 and 4 indeces
- /*
- Substr() is similar to slice().
- The difference is that the second parameter specifies the length of the extracted part.
- */
- var str = 'Find where the string starts';
- var res = str.slice(0, 10);
- console.log(res); // prints 'Find where'
- /*
- The replace() method replaces a specified value with another value in a string.
- This method can also take a regular expression as the search value.
- By default, the replace() function replaces only the first match.
- To replace all matches, use a regular expression with a g flag (for global match)
- */
- var str = 'Find where the string starts';
- var res = str.replace('Find', 'Stop'); // replace 'Find' with 'Stop'
- console.log(res); // prints 'Stop where the string starts'
- var str = 'Find the string and replace the string';
- var res = str.replace(/string/g, 'candy'); // using g flag
- console.log(res); // prints 'Find the candy and replace the candy'
- /*
- We can convert to lower case or upper case using these methods: toLowerCase() and toUpperCase.
- */
- var str = 'Find Where The String Starts';
- var res = str.toLowerCase();
- console.log(res); // prints 'find where the string starts'
- var str = 'find where the string starts';
- var res = str.toUpperCase();
- console.log(res); // prints 'FIND WHERE THE STRING STARTS'
- //Concat() joins two or more strings:
- var str = 'Bye';
- var str2 = 'Bye!';
- var res = str.concat(" ", str2);
- console.log(res); // prints 'Bye Bye!'
- /*
- CharAt() method returns the character at a specified index (position) in a string.
- CharCodeAt() method returns the unicode of the character at a specified index in a string.
- */
- var str = 'String';
- var res = str.charAt(0); // asking for the first character
- console.log(res); // prints the first character 'S'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement