In my previous article on ES6 array methods, I introduced the new methods available in ECMAScript 6 that work with the Array
type. In this tutorial, you'll learn about new ES6 methods that work with strings: String.prototype.*
We'll develop several examples, and mention the polyfills available for them. Remember that if you want to polyfill them all using a single library, you can employ es6-shim by Paul Miller.
String.prototype.startsWith()
One of the most-used functions in every modern programming language is the one to verify if a string starts with a given substring. Before ES6, JavaScript had no such function, meaning you had to write it yourself. The following code shows how developers usually polyfilled it:
if (typeof String.prototype.startsWith !== 'function') {
String.prototype.startsWith = function (str){
return this.indexOf(str) === 0;
};
}
Or, alternatively:
if (typeof String.prototype.startsWith !== 'function') {
String.prototype.startsWith = function (str){
return this.substring(0, str.length) === str;
};
}
These snippets are still valid, but they don't reproduce exactly what the newly available String.prototype.startsWith()
method does. The new method has the following syntax:
String.prototype.startsWith(searchString[, position]);
You can see that, in addition to a substring, it accepts a second argument. The searchString
parameter specifies the substring you want to verify is the start of the string. position
indicates the position at which to start the search. The default value of position
is 0. The methods returns true
if the string starts with the provided substring, and false
otherwise. Remember that the method is case sensitive, so “Hello” is different from “hello”.
An example use of this method is shown below:
const str = 'hello!';
let result = str.startsWith('he');
// prints "true"
console.log(result);
// verify starting from the third character
result = str.startsWith('ll', 2);
// prints "true"
console.log(result);
A live demo of the previous code is shown below and also available at JSBin.
ES6 in Action: New String Methods on jsbin.com
The method is supported in Node and all modern browsers, with the exception of Internet Explorer. If you need to support older browsers, a polyfill for this method can be found in the method's page on MDN. Another polyfill has also been developed by Mathias Bynens.
String.prototype.endsWith()
In addition to String.prototype.startsWith()
, ECMAScript 6 introduces the String.prototype.endsWith()
method. It verifies that a string terminates with a given substring. The syntax of this method, shown below, is very similar to String.prototype.startsWith()
:
String.prototype.endsWith(searchString[, position]);
As you can see, this method accepts the same parameters as String.prototype.startsWith()
, and also returns the same type of values.
A difference is that the position
parameter lets you search within the string as if the string were only this long. In other words, if we have the string house
and we call the method with 'house'.endsWith('us', 4)
, we obtain true
, because it's like we actually had the string hous
(note the missing “e”).
An example use of this method is shown below:
const str = 'hello!';
const result = str.endsWith('lo!');
// prints "true"
console.log(result);
// verify as if the string was "hell"
result = str.endsWith('lo!', 5);
// prints "false"
console.log(result);
A live demo of the previous snippet is shown below and is also available at JSBin.
ES6 in Action: New String Methods on jsbin.com
The method is supported in Node and all modern browsers, with the exception of Internet Explorer. If you need to support older browsers, a polyfill for this method can be found in the method's page on MDN. Another polyfill has been developed by Mathias Bynens.
Continue reading %ES6 in Action: New String Methods — String.prototype.*%
by Aurelio De Rosa via SitePoint
No comments:
Post a Comment