String

Strings already have quite a few helpers prior to ES6, but even more have been added to the mix.

Unicode Functions

“Unicode-Aware String Operations” in Chapter 2 discusses String.fromCodePoint(..), String#codePointAt(..), and String#normalize(..) in detail. They have been added to improve Unicode support in JS string values.

  1. String.fromCodePoint( 0x1d49e ); // "?"
  2. "ab?d".codePointAt( 2 ).toString( 16 ); // "1d49e"

The normalize(..) string prototype method is used to perform Unicode normalizations that either combine characters with adjacent “combining marks” or decompose combined characters.

Generally, the normalization won’t create a visible effect on the contents of the string, but will change the contents of the string, which can affect how things like the length property are reported, as well as how character access by position behave:

  1. var s1 = "e\u0301";
  2. s1.length; // 2
  3. var s2 = s1.normalize();
  4. s2.length; // 1
  5. s2 === "\xE9"; // true

normalize(..) takes an optional argument that specifies the normalization form to use. This argument must be one of the following four values: "NFC" (default), "NFD", "NFKC", or "NFKD".

Note: Normalization forms and their effects on strings is well beyond the scope of what we’ll discuss here. See “Unicode Normalization Forms” (http://www.unicode.org/reports/tr15/) for more information.

String.raw(..) Static Function

The String.raw(..) utility is provided as a built-in tag function to use with template string literals (see Chapter 2) for obtaining the raw string value without any processing of escape sequences.

This function will almost never be called manually, but will be used with tagged template literals:

  1. var str = "bc";
  2. String.raw`\ta${str}d\xE9`;
  3. // "\tabcd\xE9", not " abcdé"

In the resultant string, \ and t are separate raw characters, not the one escape sequence character \t. The same is true with the Unicode escape sequence.

repeat(..) Prototype Function

In languages like Python and Ruby, you can repeat a string as:

  1. "foo" * 3; // "foofoofoo"

That doesn’t work in JS, because * multiplication is only defined for numbers, and thus "foo" coerces to the NaN number.

However, ES6 defines a string prototype method repeat(..) to accomplish the task:

  1. "foo".repeat( 3 ); // "foofoofoo"

String Inspection Functions

In addition to String#indexOf(..) and String#lastIndexOf(..) from prior to ES6, three new methods for searching/inspection have been added: startsWith(..), endsWith(..), and includes(..).

  1. var palindrome = "step on no pets";
  2. palindrome.startsWith( "step on" ); // true
  3. palindrome.startsWith( "on", 5 ); // true
  4. palindrome.endsWith( "no pets" ); // true
  5. palindrome.endsWith( "no", 10 ); // true
  6. palindrome.includes( "on" ); // true
  7. palindrome.includes( "on", 6 ); // false

For all the string search/inspection methods, if you look for an empty string "", it will either be found at the beginning or the end of the string.

Warning: These methods will not by default accept a regular expression for the search string. See “Regular Expression Symbols” in Chapter 7 for information about disabling the isRegExp check that is performed on this first argument.