This content originally appeared on DEV Community and was authored by David Tetreau
There are many built-in methods in JavaScript that we can use to make programming easier. For Part 14, we will go over some of the more common string methods. Open up your repl and code along!
String Methods
String methods help you to work with strings. It can save a lot of time! These built-in methods handle tasks much more quickly and efficiently than doing things manually. Here are some important string methods:
.length
This is a method that will return the number of characters in a string. Here's an example:
const animal = "gorilla";
console.log(animal.length); // logs 7
The .length method checked the animal string and returned its length. Then, the number 7 was logged to the console. This represents the total number of characters in the string.
indexOf()
This method tells you the location (index) of the first time specified characters appear in a string. Here's an example:
const color = "light blue";
console.log(color.indexOf("blue")); // returns 6
This method is different from .length because .length returns the total character count, whereas .indexOf returns the index of the first character of the part of the string you're looking for. The indexes in a string start at 0. If the .indexOf method does not find the specified characters in a string, it will return -1.
lastIndexOf()
This method gives you the location (index) of the last time specified characters appear in a string. The number that is returned is the location of the first character of the specified characters which the method is searching for.
const shark = "My favorite shark is the Great White Shark";
console.log(shark.lastIndexOf("shark")); // returns 12
Notice the lastIndexOf("shark") is case sensitive. Therefore, it returns the index of 12 because that is last time the lowercase version of "shark" appears in the sentence. Also, notice that it returns the index of the first character of that word.
slice(index1, index2)
This method takes strings and returns the part you slice out of it. The method takes a start point and an end point (indexes). To use this method, you have to know where the substring you are interested in starts. Here’s an example:
let superheroes = "Superman";
console.log(superheroes.slice(1 , 3)); //up
console.log(superheroes); //Superman
If you run this code, you will see that when you slice from index 1 to index 3 in the array, it just makes a copy of that part of the array without changing the original array. You can see this by running console.log() on the original array after slicing. If you only provide one parameter, the remainder of the string will be sliced. If you provide a negative parameter, the slice will start from the end of the string and work backwards.
substring()
According to MDN Web Docs "The substring() and slice() methods are almost identical, but there are a couple of subtle differences between the two." Here are the differences:
- With the substring method, if the first index is greater than the second index, the arguments are automatically swapped. The slice method will return an empty string.
- The substring method automatically treats arguments that are negative or NAN as being the number 0. Whereas, with the slice method, negative values are counted backwards from the end of the string. Here's an example:
let movie = "Soul";
console.log(movie.slice(0, -1)); // Sou
console.log(movie.substring(0, -1)); // becomes (0, 0)
replace(" "," ")
This method is very simple. It replaces a group of characters in a string and substitutes another. Here's an example:
let favoriteColor = "My favorite color is green";
favoriteColor.replace("green", "blue");
toUpperCase() and toLowerCase()
A string is converted to upper case with toUpperCase() and a string is converted to lower case with toLowerCase(). Here is an example of both of these methods:
let alphabet = "a, B, c, D, e, F, g, H, i, J, k, L, m";
console.log(alphabet.toUpperCase());// all uppercase
console.log(alphabet.toLowerCase());// all lowercase
concat()
concat() joins strings together. Here is an example:
let brand = "Microsoft";
let system = " Windows";
let operatingSystem = brand.concat(system);
console.log(operatingSystem);
This method is another way to accomplish string concatenation which we learned about in a previous post.
I hope you have enjoyed this post! Please check out the entire "JavaScript Made Easy" series by David Tetreau. There will be a new post daily.
This content originally appeared on DEV Community and was authored by David Tetreau
David Tetreau | Sciencx (2021-05-16T02:48:51+00:00) JavaScript Made Easy: Part 14. Retrieved from https://www.scien.cx/2021/05/16/javascript-made-easy-part-14/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.