New string methods:
ES6 has a new kind of string literal, the template literal:
In ECMAScript 6, there is a new kind of Unicode escape that lets you specify any code point (even those beyond 16 bits):
More information on escapes is given in the chapter on Unicode.
Template literals are described in depth in their own chapter. They provide three interesting features.
First, template literals support string interpolation:
Second, template literals can contain multiple lines:
Third, template literals are “raw” if you prefix them with the tag String.raw
– the backslash is not a special character and escapes such as \n
are not interpreted:
Strings are iterable, which means that you can use for-of
to iterate over their characters:
And you can use the spread operator (...
) to turn strings into Arrays:
The string iterator splits strings along code point boundaries, which means that the strings it returns comprise one or two JavaScript characters:
Iteration gives you a quick way to count the Unicode code points in a string:
Iteration also helps with reversing strings that contain non-BMP code points (which are larger than 16 bit and encoded as two JavaScript characters):
The new method codePointAt()
returns the numeric value of a code point at a given index in a string:
This method works well when combined with iteration over strings:
The opposite of codePointAt()
is String.fromCodePoint()
:
Three new methods check whether a string exists within another string:
Each of these methods has a position as an optional second parameter, which specifies where the string to be searched starts or ends:
The repeat()
method repeats strings:
In ES6, the four string methods that accept regular expression parameters do relatively little. They mainly call methods of their parameters:
String.prototype.match(regexp)
callsregexp[Symbol.match](this)
.String.prototype.replace(searchValue, replaceValue)
callssearchValue[Symbol.replace](this, replaceValue)
.String.prototype.search(regexp)
callsregexp[Symbol.search](this)
.String.prototype.split(separator, limit)
callsseparator[Symbol.split](this, limit)
.The parameters don’t have to be regular expressions, anymore. Any objects with appropriate methods will do.
Tagged templates:
String.raw(callSite, ...substitutions) : string
Consult the chapter on template literals for more information.
Unicode and code points:
String.fromCodePoint(...codePoints : number[]) : string
String.prototype.codePointAt(pos) : number
pos
(comprising one or two JavaScript characters).String.prototype.normalize(form? : string) : string
'NFC'
form is recommended for general text.Finding strings:
String.prototype.startsWith(searchString, position=0) : boolean
searchString
? position
lets you specify where the string to be checked starts.String.prototype.endsWith(searchString, endPosition=searchString.length) : boolean
searchString
? endPosition
lets you specify where the string to be checked ends.String.prototype.includes(searchString, position=0) : boolean
searchString
? position
lets you specify where the string to be searched starts.Repeating strings:
String.prototype.repeat(count) : string
count
times.