|

New String Methods In ES6

New String Methods In ES6

One area of JavaScript has definitely needed some help with in the past is working with strings. Traditionally, there hasn’t been a lot of built in methods to deal with common string processing scenarios. You could get things done with regular expressions just fine, but it would be nice to have methods dedicated to string processing much like covered with all of the PHP String Functions. We don’t quite have the kitchen sink now in JavaScript, but there are many useful additions and we’ll check them out in this article.


string.startsWith()

The startsWith() method operates like so: stringOne.startsWith( stringTwo ) is true if and only if stringOne starts with stringTwo. With this understanding, we can set up a few examples here of checking a string to see if it starts with a particular different string.


string.endsWith()

Similar to the startsWith() method, we also have an endsWith() method. These are great little methods since it is common to check a string for beginning or ending values. The endsWith() method works like so. stringOne.endsWith( stringTwo ) is true if and only if stringOne ends with stringTwo. Let’s again look at a few example of this in action.


string.includes()

How often do you need to check if a string contains another string? Like every day, amiright? Well now you have this handy method in ES6 to do this quickly. It works as follows. stringOne.includes( stringTwo ) is true if and only if stringTwo is a substring of stringOne. Let’s test it out now.


string.repeat()

The string.repeat() method works like this. string.repeat( num ) replicates string, num times, and joins them together.


New String Methods In ES6 Summary

In this tutorial, we learned a bit about the new string methods available to developers in ES6. While not quite as incredible as some of the other new features we’ve been covering lately, hopefully you do find this additions to the language helpful!