Today, in this post we’ll discuss some of the string methods which will make you a better developer and also save your time and day.
So, let’s get started.
Strings methods we’ll look at today,
#1 Split –
let split = "Game of Thrones".split(" "); console.log(split); //['Game', 'of', 'Thrones']
split() method, splits a string into it’s substring and returns a new array. As you can see in above example, we separated the words as an individual word into a new array.
Useful to count the number of words, or to separate the words.
#2 Slice –
let slice = "Joey Tribbiani".slice(0, 4); console.log(slice); //Joey
slice() method, returns elements with the given indexes, the two parameters refer to the start(0) and end(4). As you can see above, we’ve got the Joey, as the start is
0 index and end it before the
slice() method has much more use cases, you can also use this for Array. The best real-world example I can give is of the Twitter 240 characters limit, you can only get the desired amount of characters using this method.
#3 toUpperCase –
let upperCase = "tyrion lannister".toUpperCase(); console.log(upperCase); //Tyrion Lannister
toUpperCase() method, just as the name suggests converts a string into the upper case or in the Capital letter.
This method is useful when getting data from the user, you can make certain words into upper case and get the data into your database.
This can also be done in CSS but it’s good practice to do in JS.
#4 ToLowerCase –
let lowerCase = "SQUID GAME".toLowerCase(); console.log(lowerCase); //squid game
toLowerCase() is just the opposite of the
toUpperCase() method, which will convert upper case letters into the lower case.
I personally use this method mostly on the Nodejs side, when getting the parameters in the strings, I usually prefer to get it into the lower case to avoid errors.
#5 Replace –
let title = "Attack on Titan"; let replace = title.replace("Titan", "Cats"); console.log(replace); //Attack on Titan
replace() method returns a new string with the replaced string. As you can see in the above example, we are replacing the
Cats and returning a new string.
There are even more uses of
replace() like, we can use Regular Expressions, and it doesn’t change the original string, so you can always have an original value.
#6 IndexOf –
let indexOf = "Android is better than Iphone".indexOf("Iphone"); console.log(indexOf); //23
indexOf() method returns the index or the positing of the given string or value. There are many use cases of this method like you can analyze the URL string or even you can check whether the value exists or not.
If there’s no value, it’ll return
#7 Trim –
let trim = " 221B Baker Street".trim(); console.log(trim); //221B Baker Street
trim() method cleans up the string by removing the empty white spaces and returns a new string. It’s best used when getting user data from the input, you don’t want to get the false white spacings from the user.
And, that’s it, sure there are many more methods, but these can be more helpful for your upcoming projects. Let me know if you think there are methods that we should’ve mentioned, we’d be glad to update our post.
If you liked this post make sure to share it with your friends and also make sure to follow our Coding Master newsletter.
- Frontegg: Best Auth System for your Frontend - September 27, 2022
- NextJS vs ReactJS: Which is the best for your project? - September 26, 2022
- 5 Amazing React libraries to make your App Stand Out!￼ - July 28, 2022