22.1 String Objects
22.1.1 The String Constructor
The String
- is %String%.
- is the initial value of the
"String" property of theglobal object . - creates and initializes a new String object when called as a
constructor . - performs a type conversion when called as a function rather than as a
constructor . - may be used as the value of an
extends
clause of a class definition. Subclassconstructors that intend to inherit the specified String behaviour must include asuper
call to the Stringconstructor to create and initialize the subclass instance with a [[StringData]] internal slot.
22.1.1.1 String ( value )
This function performs the following steps when called:
- If value is not present, then
- Let s be the empty String.
- Else,
- If NewTarget is
undefined and valueis a Symbol , returnSymbolDescriptiveString (value). - Let s be ?
ToString (value).
- If NewTarget is
- If NewTarget is
undefined , return s. - Return
StringCreate (s, ?GetPrototypeFromConstructor (NewTarget,"%String.prototype%" )).
22.1.2 Properties of the String Constructor
The String
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
22.1.2.1 String.fromCharCode ( ...codeUnits )
This function may be called with any number of arguments which form the rest parameter codeUnits.
It performs the following steps when called:
- Let result be the empty String.
- For each element next of codeUnits, do
- Let nextCU be the code unit whose numeric value is
ℝ (?ToUint16 (next)). - Set result to the
string-concatenation of result and nextCU.
- Let nextCU be the code unit whose numeric value is
- Return result.
The
22.1.2.2 String.fromCodePoint ( ...codePoints )
This function may be called with any number of arguments which form the rest parameter codePoints.
It performs the following steps when called:
- Let result be the empty String.
- For each element next of codePoints, do
- Let nextCP be ?
ToNumber (next). - If
IsIntegralNumber (nextCP) isfalse , throw aRangeError exception. - If
ℝ (nextCP) < 0 orℝ (nextCP) > 0x10FFFF, throw aRangeError exception. - Set result to the
string-concatenation of result andUTF16EncodeCodePoint (ℝ (nextCP)).
- Let nextCP be ?
Assert : If codePoints is empty, then result is the empty String.- Return result.
The
22.1.2.3 String.prototype
The initial value of String.prototype
is the
This property has the attributes { [[Writable]]:
22.1.2.4 String.raw ( template, ...substitutions )
This function may be called with a variable number of arguments. The first argument is template and the remainder of the arguments form the
It performs the following steps when called:
- Let substitutionCount be the number of elements in substitutions.
- Let cooked be ?
ToObject (template). - Let literals be ?
ToObject (?Get (cooked,"raw" )). - Let literalCount be ?
LengthOfArrayLike (literals). - If literalCount ≤ 0, return the empty String.
- Let R be the empty String.
- Let nextIndex be 0.
- Repeat,
- Let nextLiteralVal be ?
Get (literals, !ToString (𝔽 (nextIndex))). - Let nextLiteral be ?
ToString (nextLiteralVal). - Set R to the
string-concatenation of R and nextLiteral. - If nextIndex + 1 = literalCount, return R.
- If nextIndex < substitutionCount, then
- Let nextSubVal be substitutions[nextIndex].
- Let nextSub be ?
ToString (nextSubVal). - Set R to the
string-concatenation of R and nextSub.
- Set nextIndex to nextIndex + 1.
- Let nextLiteralVal be ?
This function is intended for use as a tag function of a Tagged Template (
22.1.3 Properties of the String Prototype Object
The String prototype object:
- is %String.prototype%.
is a String exotic object and has the internal methods specified for such objects.- has a [[StringData]] internal slot whose value is the empty String.
- has a
"length" property whose initial value is+0 𝔽 and whose attributes are { [[Writable]]:false , [[Enumerable]]:false , [[Configurable]]:false }. - has a [[Prototype]] internal slot whose value is
%Object.prototype% .
Unless explicitly stated otherwise, the methods of the String prototype object defined below are not generic and the
22.1.3.1 String.prototype.at ( index )
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let len be the length of S.
- Let relativeIndex be ?
ToIntegerOrInfinity (index). - If relativeIndex ≥ 0, then
- Let k be relativeIndex.
- Else,
- Let k be len + relativeIndex.
- If k < 0 or k ≥ len, return
undefined . - Return the
substring of S from k to k + 1.
22.1.3.2 String.prototype.charAt ( pos )
This method returns a single element String containing the code unit at index pos within the String value resulting from converting this object to a String. If there is no element at that index, the result is the empty String. The result
If pos
is an x.charAt(pos)
is equivalent to the result of x.substring(pos, pos + 1)
.
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let position be ?
ToIntegerOrInfinity (pos). - Let size be the length of S.
- If position < 0 or position ≥ size, return the empty String.
- Return the
substring of S from position to position + 1.
This method is intentionally generic; it does not require that its
22.1.3.3 String.prototype.charCodeAt ( pos )
This method returns a Number (a non-negative
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let position be ?
ToIntegerOrInfinity (pos). - Let size be the length of S.
- If position < 0 or position ≥ size, return
NaN . - Return the
Number value for the numeric value of the code unit at index position within the String S.
This method is intentionally generic; it does not require that its
22.1.3.4 String.prototype.codePointAt ( pos )
This method returns a non-negative
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let position be ?
ToIntegerOrInfinity (pos). - Let size be the length of S.
- If position < 0 or position ≥ size, return
undefined . - Let cp be
CodePointAt (S, position). - Return
𝔽 (cp.[[CodePoint]]).
This method is intentionally generic; it does not require that its
22.1.3.5 String.prototype.concat ( ...args )
When this method is called it returns the String value consisting of the code units of the
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let R be S.
- For each element next of args, do
- Let nextString be ?
ToString (next). - Set R to the
string-concatenation of R and nextString.
- Let nextString be ?
- Return R.
The
This method is intentionally generic; it does not require that its
22.1.3.6 String.prototype.constructor
The initial value of String.prototype.constructor
is
22.1.3.7 String.prototype.endsWith ( searchString [ , endPosition ] )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let isRegExp be ?
IsRegExp (searchString). - If isRegExp is
true , throw aTypeError exception. - Let searchStr be ?
ToString (searchString). - Let len be the length of S.
- If endPosition is
undefined , let pos be len; else let pos be ?ToIntegerOrInfinity (endPosition). - Let end be the result of
clamping pos between 0 and len. - Let searchLength be the length of searchStr.
- If searchLength = 0, return
true . - Let start be end - searchLength.
- If start < 0, return
false . - Let substring be the
substring of S from start to end. - If substring is searchStr, return
true . - Return
false .
This method returns
Throwing an exception if the first argument is a RegExp is specified in order to allow future editions to define extensions that allow such argument values.
This method is intentionally generic; it does not require that its
22.1.3.8 String.prototype.includes ( searchString [ , position ] )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let isRegExp be ?
IsRegExp (searchString). - If isRegExp is
true , throw aTypeError exception. - Let searchStr be ?
ToString (searchString). - Let pos be ?
ToIntegerOrInfinity (position). Assert : If position isundefined , then pos is 0.- Let len be the length of S.
- Let start be the result of
clamping pos between 0 and len. - Let index be
StringIndexOf (S, searchStr, start). - If index ≠ -1, return
true . - Return
false .
If searchString appears as a
Throwing an exception if the first argument is a RegExp is specified in order to allow future editions to define extensions that allow such argument values.
This method is intentionally generic; it does not require that its
22.1.3.9 String.prototype.indexOf ( searchString [ , position ] )
If searchString appears as a
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let searchStr be ?
ToString (searchString). - Let pos be ?
ToIntegerOrInfinity (position). Assert : If position isundefined , then pos is 0.- Let len be the length of S.
- Let start be the result of
clamping pos between 0 and len. - Return
𝔽 (StringIndexOf (S, searchStr, start)).
This method is intentionally generic; it does not require that its
22.1.3.10 String.prototype.isWellFormed ( )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Return
IsStringWellFormedUnicode (S).
22.1.3.11 String.prototype.lastIndexOf ( searchString [ , position ] )
If searchString appears as a
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let searchStr be ?
ToString (searchString). - Let numPos be ?
ToNumber (position). Assert : If position isundefined , then numPos isNaN .- If numPos is
NaN , let pos be +∞; otherwise, let pos be !ToIntegerOrInfinity (numPos). - Let len be the length of S.
- Let searchLen be the length of searchStr.
- Let start be the result of
clamping pos between 0 and len - searchLen. - If searchStr is the empty String, return
𝔽 (start). - For each
integer i such that 0 ≤ i ≤ start, in descending order, do - Return
-1 𝔽.
This method is intentionally generic; it does not require that its
22.1.3.12 String.prototype.localeCompare ( that [ , reserved1 [ , reserved2 ] ] )
An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement this method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of this method is used:
This method returns a Number other than
Before performing the comparisons, this method performs the following steps to prepare the Strings:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let thatValue be ?
ToString (that).
The meaning of the optional second and third parameters to this method are defined in the ECMA-402 specification; implementations that do not include ECMA-402 support must not assign any other interpretation to those parameter positions.
The actual return values are
This method itself is not directly suitable as an argument to Array.prototype.sort
because the latter requires a function of two arguments.
This method may rely on whatever language- and/or locale-sensitive comparison functionality is available to the ECMAScript environment from the
// Å ANGSTROM SIGN vs.
// Å LATIN CAPITAL LETTER A + COMBINING RING ABOVE
"\u212B".localeCompare("A\u030A")
// Ω OHM SIGN vs.
// Ω GREEK CAPITAL LETTER OMEGA
"\u2126".localeCompare("\u03A9")
// ṩ LATIN SMALL LETTER S WITH DOT BELOW AND DOT ABOVE vs.
// ṩ LATIN SMALL LETTER S + COMBINING DOT ABOVE + COMBINING DOT BELOW
"\u1E69".localeCompare("s\u0307\u0323")
// ḍ̇ LATIN SMALL LETTER D WITH DOT ABOVE + COMBINING DOT BELOW vs.
// ḍ̇ LATIN SMALL LETTER D WITH DOT BELOW + COMBINING DOT ABOVE
"\u1E0B\u0323".localeCompare("\u1E0D\u0307")
// 가 HANGUL CHOSEONG KIYEOK + HANGUL JUNGSEONG A vs.
// 가 HANGUL SYLLABLE GA
"\u1100\u1161".localeCompare("\uAC00")
For a definition and discussion of canonical equivalence see the Unicode Standard, chapters 2 and 3, as well as Unicode Standard Annex #15, Unicode Normalization Forms and Unicode Technical Note #5, Canonical Equivalence in Applications. Also see Unicode Technical Standard #10, Unicode Collation Algorithm.
It is recommended that this method should not honour Unicode compatibility equivalents or compatibility decompositions as defined in the Unicode Standard, chapter 3, section 3.7.
This method is intentionally generic; it does not require that its
22.1.3.13 String.prototype.match ( regexp )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - If regexp is neither
undefined nornull , then - Let S be ?
ToString (O). - Let rx be ?
RegExpCreate (regexp,undefined ). - Return ?
Invoke (rx,@@match , « S »).
This method is intentionally generic; it does not require that its
22.1.3.14 String.prototype.matchAll ( regexp )
This method performs a regular expression match of the String representing the
It performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - If regexp is neither
undefined nornull , then- Let isRegExp be ?
IsRegExp (regexp). - If isRegExp is
true , then- Let flags be ?
Get (regexp,"flags" ). - Perform ?
RequireObjectCoercible (flags). - If ?
ToString (flags) does not contain"g" , throw aTypeError exception.
- Let flags be ?
- Let matcher be ?
GetMethod (regexp,@@matchAll ). - If matcher is not
undefined , then- Return ?
Call (matcher, regexp, « O »).
- Return ?
- Let isRegExp be ?
- Let S be ?
ToString (O). - Let rx be ?
RegExpCreate (regexp,"g" ). - Return ?
Invoke (rx,@@matchAll , « S »).
String.prototype.split
, String.prototype.matchAll
is designed to typically act without mutating its inputs.22.1.3.15 String.prototype.normalize ( [ form ] )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - If form is
undefined , let f be"NFC" . - Else, let f be ?
ToString (form). - If f is not one of
"NFC" ,"NFD" ,"NFKC" , or"NFKD" , throw aRangeError exception. - Let ns be the String value that is the result of normalizing S into the normalization form named by f as specified in the latest Unicode Standard, Normalization Forms.
- Return ns.
This method is intentionally generic; it does not require that its
22.1.3.16 String.prototype.padEnd ( maxLength [ , fillString ] )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Return ?
StringPaddingBuiltinsImpl (O, maxLength, fillString,end ).
22.1.3.17 String.prototype.padStart ( maxLength [ , fillString ] )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Return ?
StringPaddingBuiltinsImpl (O, maxLength, fillString,start ).
22.1.3.17.1 StringPaddingBuiltinsImpl ( O, maxLength, fillString, placement )
The abstract operation StringPaddingBuiltinsImpl takes arguments O (an
- Let S be ?
ToString (O). - Let intMaxLength be
ℝ (?ToLength (maxLength)). - Let stringLength be the length of S.
- If intMaxLength ≤ stringLength, return S.
- If fillString is
undefined , set fillString to the String value consisting solely of the code unit 0x0020 (SPACE). - Else, set fillString to ?
ToString (fillString). - Return
StringPad (S, intMaxLength, fillString, placement).
22.1.3.17.2 StringPad ( S, maxLength, fillString, placement )
The abstract operation StringPad takes arguments S (a String), maxLength (a non-negative
- Let stringLength be the length of S.
- If maxLength ≤ stringLength, return S.
- If fillString is the empty String, return S.
- Let fillLen be maxLength - stringLength.
- Let truncatedStringFiller be the String value consisting of repeated concatenations of fillString truncated to length fillLen.
- If placement is
start , return thestring-concatenation of truncatedStringFiller and S. - Else, return the
string-concatenation of S and truncatedStringFiller.
The argument maxLength will be clamped such that it can be no smaller than the length of S.
The argument fillString defaults to
22.1.3.17.3 ToZeroPaddedDecimalString ( n, minLength )
The abstract operation ToZeroPaddedDecimalString takes arguments n (a non-negative
- Let S be the String representation of n, formatted as a decimal number.
- Return
StringPad (S, minLength,"0" ,start ).
22.1.3.18 String.prototype.repeat ( count )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let n be ?
ToIntegerOrInfinity (count). - If n < 0 or n = +∞, throw a
RangeError exception. - If n = 0, return the empty String.
- Return the String value that is made from n copies of S appended together.
This method creates the String value consisting of the code units of the
This method is intentionally generic; it does not require that its
22.1.3.19 String.prototype.replace ( searchValue, replaceValue )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - If searchValue is neither
undefined nornull , then - Let string be ?
ToString (O). - Let searchString be ?
ToString (searchValue). - Let functionalReplace be
IsCallable (replaceValue). - If functionalReplace is
false , then- Set replaceValue to ?
ToString (replaceValue).
- Set replaceValue to ?
- Let searchLength be the length of searchString.
- Let position be
StringIndexOf (string, searchString, 0). - If position = -1, return string.
- Let preceding be the
substring of string from 0 to position. - Let following be the
substring of string from position + searchLength. - If functionalReplace is
true , then - Else,
Assert : replaceValueis a String .- Let captures be a new empty
List . - Let replacement be !
GetSubstitution (searchString, string, position, captures,undefined , replaceValue).
- Return the
string-concatenation of preceding, replacement, and following.
This method is intentionally generic; it does not require that its
22.1.3.19.1 GetSubstitution ( matched, str, position, captures, namedCaptures, replacementTemplate )
The abstract operation GetSubstitution takes arguments matched (a String), str (a String), position (a non-negative
- Let stringLength be the length of str.
Assert : position ≤ stringLength.- Let result be the empty String.
- Let templateRemainder be replacementTemplate.
- Repeat, while templateRemainder is not the empty String,
- NOTE: The following steps isolate ref (a prefix of templateRemainder), determine refReplacement (its replacement), and then append that replacement to result.
- If templateRemainder starts with
"$$" , then- Let ref be
"$$" . - Let refReplacement be
"$" .
- Let ref be
- Else if templateRemainder starts with
"$`" , then- Let ref be
"$`" . - Let refReplacement be the
substring of str from 0 to position.
- Let ref be
- Else if templateRemainder starts with
"$&" , then- Let ref be
"$&" . - Let refReplacement be matched.
- Let ref be
- Else if templateRemainder starts with
"$'" (0x0024 (DOLLAR SIGN) followed by 0x0027 (APOSTROPHE)), then- Let ref be
"$'" . - Let matchLength be the length of matched.
- Let tailPos be position + matchLength.
- Let refReplacement be the
substring of str frommin (tailPos, stringLength). - NOTE: tailPos can exceed stringLength only if this abstract operation was invoked by a call to the intrinsic
@@replace method of%RegExp.prototype% on an object whose"exec" property is not the intrinsic %RegExp.prototype.exec%.
- Let ref be
- Else if templateRemainder starts with
"$" followed by 1 or more decimal digits, then- If templateRemainder starts with
"$" followed by 2 or more decimal digits, let digitCount be 2. Otherwise, let digitCount be 1. - Let digits be the
substring of templateRemainder from 1 to 1 + digitCount. - Let index be
ℝ (StringToNumber (digits)). Assert : 0 ≤ index ≤ 99.- Let captureLen be the number of elements in captures.
- If index > captureLen and digitCount = 2, then
- NOTE: When a two-digit replacement pattern specifies an index exceeding the count of capturing groups, it is treated as a one-digit replacement pattern followed by a literal digit.
- Set digitCount to 1.
- Set digits to the
substring of digits from 0 to 1. - Set index to
ℝ (StringToNumber (digits)).
- Let ref be the
substring of templateRemainder from 0 to 1 + digitCount. - If 1 ≤ index ≤ captureLen, then
- Let capture be captures[index - 1].
- If capture is
undefined , then- Let refReplacement be the empty String.
- Else,
- Let refReplacement be capture.
- Else,
- Let refReplacement be ref.
- If templateRemainder starts with
- Else if templateRemainder starts with
"$<" , then- Let gtPos be
StringIndexOf (templateRemainder,">" , 0). - If gtPos = -1 or namedCaptures is
undefined , then- Let ref be
"$<" . - Let refReplacement be ref.
- Let ref be
- Else,
- Let ref be the
substring of templateRemainder from 0 to gtPos + 1. - Let groupName be the
substring of templateRemainder from 2 to gtPos. Assert : namedCapturesis an Object .- Let capture be ?
Get (namedCaptures, groupName). - If capture is
undefined , then- Let refReplacement be the empty String.
- Else,
- Let refReplacement be ?
ToString (capture).
- Let refReplacement be ?
- Let ref be the
- Let gtPos be
- Else,
- Let ref be the
substring of templateRemainder from 0 to 1. - Let refReplacement be ref.
- Let ref be the
- Let refLength be the length of ref.
- Set templateRemainder to the
substring of templateRemainder from refLength. - Set result to the
string-concatenation of result and refReplacement.
- Return result.
22.1.3.20 String.prototype.replaceAll ( searchValue, replaceValue )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - If searchValue is neither
undefined nornull , then- Let isRegExp be ?
IsRegExp (searchValue). - If isRegExp is
true , then- Let flags be ?
Get (searchValue,"flags" ). - Perform ?
RequireObjectCoercible (flags). - If ?
ToString (flags) does not contain"g" , throw aTypeError exception.
- Let flags be ?
- Let replacer be ?
GetMethod (searchValue,@@replace ). - If replacer is not
undefined , then- Return ?
Call (replacer, searchValue, « O, replaceValue »).
- Return ?
- Let isRegExp be ?
- Let string be ?
ToString (O). - Let searchString be ?
ToString (searchValue). - Let functionalReplace be
IsCallable (replaceValue). - If functionalReplace is
false , then- Set replaceValue to ?
ToString (replaceValue).
- Set replaceValue to ?
- Let searchLength be the length of searchString.
- Let advanceBy be
max (1, searchLength). - Let matchPositions be a new empty
List . - Let position be
StringIndexOf (string, searchString, 0). - Repeat, while position ≠ -1,
- Append position to matchPositions.
- Set position to
StringIndexOf (string, searchString, position + advanceBy).
- Let endOfLastMatch be 0.
- Let result be the empty String.
- For each element p of matchPositions, do
- Let preserved be the
substring of string from endOfLastMatch to p. - If functionalReplace is
true , then - Else,
Assert : replaceValueis a String .- Let captures be a new empty
List . - Let replacement be !
GetSubstitution (searchString, string, p, captures,undefined , replaceValue).
- Set result to the
string-concatenation of result, preserved, and replacement. - Set endOfLastMatch to p + searchLength.
- Let preserved be the
- If endOfLastMatch < the length of string, then
- Set result to the
string-concatenation of result and thesubstring of string from endOfLastMatch.
- Set result to the
- Return result.
22.1.3.21 String.prototype.search ( regexp )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - If regexp is neither
undefined nornull , then - Let string be ?
ToString (O). - Let rx be ?
RegExpCreate (regexp,undefined ). - Return ?
Invoke (rx,@@search , « string »).
This method is intentionally generic; it does not require that its
22.1.3.22 String.prototype.slice ( start, end )
This method returns a
It performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let len be the length of S.
- Let intStart be ?
ToIntegerOrInfinity (start). - If intStart = -∞, let from be 0.
- Else if intStart < 0, let from be
max (len + intStart, 0). - Else, let from be
min (intStart, len). - If end is
undefined , let intEnd be len; else let intEnd be ?ToIntegerOrInfinity (end). - If intEnd = -∞, let to be 0.
- Else if intEnd < 0, let to be
max (len + intEnd, 0). - Else, let to be
min (intEnd, len). - If from ≥ to, return the empty String.
- Return the
substring of S from from to to.
This method is intentionally generic; it does not require that its
22.1.3.23 String.prototype.split ( separator, limit )
This method returns an Array into which substrings of the result of converting this object to a String have been stored. The substrings are determined by searching from left to right for occurrences of separator; these occurrences are not part of any String in the returned array, but serve to divide up the String value. The value of separator may be a String of any length or it may be an object, such as a RegExp, that has a
It performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - If separator is neither
undefined nornull , then - Let S be ?
ToString (O). - If limit is
undefined , let lim be 232 - 1; else let lim beℝ (?ToUint32 (limit)). - Let R be ?
ToString (separator). - If lim = 0, then
- Return
CreateArrayFromList (« »).
- Return
- If separator is
undefined , then- Return
CreateArrayFromList (« S »).
- Return
- Let separatorLength be the length of R.
- If separatorLength = 0, then
- Let head be the
substring of S from 0 to lim. - Let codeUnits be a
List consisting of the sequence of code units that are the elements of head. - Return
CreateArrayFromList (codeUnits).
- Let head be the
- If S is the empty String, return
CreateArrayFromList (« S »). - Let substrings be a new empty
List . - Let i be 0.
- Let j be
StringIndexOf (S, R, 0). - Repeat, while j ≠ -1,
- Let T be the
substring of S from i to j. - Append T to substrings.
- If the number of elements in substrings is lim, return
CreateArrayFromList (substrings). - Set i to j + separatorLength.
- Set j to
StringIndexOf (S, R, i).
- Let T be the
- Let T be the
substring of S from i. - Append T to substrings.
- Return
CreateArrayFromList (substrings).
The value of separator may be an empty String. In this case, separator does not match the empty
If the
If separator is
This method is intentionally generic; it does not require that its
22.1.3.24 String.prototype.startsWith ( searchString [ , position ] )
This method performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let isRegExp be ?
IsRegExp (searchString). - If isRegExp is
true , throw aTypeError exception. - Let searchStr be ?
ToString (searchString). - Let len be the length of S.
- If position is
undefined , let pos be 0; else let pos be ?ToIntegerOrInfinity (position). - Let start be the result of
clamping pos between 0 and len. - Let searchLength be the length of searchStr.
- If searchLength = 0, return
true . - Let end be start + searchLength.
- If end > len, return
false . - Let substring be the
substring of S from start to end. - If substring is searchStr, return
true . - Return
false .
This method returns
Throwing an exception if the first argument is a RegExp is specified in order to allow future editions to define extensions that allow such argument values.
This method is intentionally generic; it does not require that its
22.1.3.25 String.prototype.substring ( start, end )
This method returns a
If either argument is
If start is strictly greater than end, they are swapped.
It performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let len be the length of S.
- Let intStart be ?
ToIntegerOrInfinity (start). - If end is
undefined , let intEnd be len; else let intEnd be ?ToIntegerOrInfinity (end). - Let finalStart be the result of
clamping intStart between 0 and len. - Let finalEnd be the result of
clamping intEnd between 0 and len. - Let from be
min (finalStart, finalEnd). - Let to be
max (finalStart, finalEnd). - Return the
substring of S from from to to.
This method is intentionally generic; it does not require that its
22.1.3.26 String.prototype.toLocaleLowerCase ( [ reserved1 [ , reserved2 ] ] )
An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement this method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of this method is used:
This method interprets a String value as a sequence of UTF-16 encoded code points, as described in
It works exactly the same as toLowerCase
except that it is intended to yield a locale-sensitive result corresponding with conventions of the
The meaning of the optional parameters to this method are defined in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
This method is intentionally generic; it does not require that its
22.1.3.27 String.prototype.toLocaleUpperCase ( [ reserved1 [ , reserved2 ] ] )
An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement this method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of this method is used:
This method interprets a String value as a sequence of UTF-16 encoded code points, as described in
It works exactly the same as toUpperCase
except that it is intended to yield a locale-sensitive result corresponding with conventions of the
The meaning of the optional parameters to this method are defined in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
This method is intentionally generic; it does not require that its
22.1.3.28 String.prototype.toLowerCase ( )
This method interprets a String value as a sequence of UTF-16 encoded code points, as described in
It performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let sText be
StringToCodePoints (S). - Let lowerText be the result of toLowercase(sText), according to the Unicode Default Case Conversion algorithm.
- Let L be
CodePointsToString (lowerText). - Return L.
The result must be derived according to the locale-insensitive case mappings in the Unicode Character Database (this explicitly includes not only the file UnicodeData.txt
, but also all locale-insensitive mappings in the file SpecialCasing.txt
that accompanies it).
The case mapping of some code points may produce multiple code points. In this case the result String may not be the same length as the source String. Because both toUpperCase
and toLowerCase
have context-sensitive behaviour, the methods are not symmetrical. In other words, s.toUpperCase().toLowerCase()
is not necessarily equal to s.toLowerCase()
.
This method is intentionally generic; it does not require that its
22.1.3.29 String.prototype.toString ( )
This method performs the following steps when called:
- Return ?
ThisStringValue (this value).
For a String object, this method happens to return the same thing as the valueOf
method.
22.1.3.30 String.prototype.toUpperCase ( )
This method interprets a String value as a sequence of UTF-16 encoded code points, as described in
It behaves in exactly the same way as String.prototype.toLowerCase
, except that the String is mapped using the toUppercase algorithm of the Unicode Default Case Conversion.
This method is intentionally generic; it does not require that its
22.1.3.31 String.prototype.toWellFormed ( )
This method returns a String representation of this object with all
It performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let S be ?
ToString (O). - Let strLen be the length of S.
- Let k be 0.
- Let result be the empty String.
- Repeat, while k < strLen,
- Let cp be
CodePointAt (S, k). - If cp.[[IsUnpairedSurrogate]] is
true , then- Set result to the
string-concatenation of result and 0xFFFD (REPLACEMENT CHARACTER).
- Set result to the
- Else,
- Set result to the
string-concatenation of result andUTF16EncodeCodePoint (cp.[[CodePoint]]).
- Set result to the
- Set k to k + cp.[[CodeUnitCount]].
- Let cp be
- Return result.
22.1.3.32 String.prototype.trim ( )
This method interprets a String value as a sequence of UTF-16 encoded code points, as described in
It performs the following steps when called:
- Let S be the
this value. - Return ?
TrimString (S,start+end ).
This method is intentionally generic; it does not require that its
22.1.3.32.1 TrimString ( string, where )
The abstract operation TrimString takes arguments string (an
- Let str be ?
RequireObjectCoercible (string). - Let S be ?
ToString (str). - If where is
start , then- Let T be the String value that is a copy of S with leading white space removed.
- Else if where is
end , then- Let T be the String value that is a copy of S with trailing white space removed.
- Else,
Assert : where isstart+end .- Let T be the String value that is a copy of S with both leading and trailing white space removed.
- Return T.
The definition of white space is the union of
22.1.3.33 String.prototype.trimEnd ( )
This method interprets a String value as a sequence of UTF-16 encoded code points, as described in
It performs the following steps when called:
- Let S be the
this value. - Return ?
TrimString (S,end ).
This method is intentionally generic; it does not require that its
22.1.3.34 String.prototype.trimStart ( )
This method interprets a String value as a sequence of UTF-16 encoded code points, as described in
It performs the following steps when called:
- Let S be the
this value. - Return ?
TrimString (S,start ).
This method is intentionally generic; it does not require that its
22.1.3.35 String.prototype.valueOf ( )
This method performs the following steps when called:
- Return ?
ThisStringValue (this value).
22.1.3.35.1 ThisStringValue ( value )
The abstract operation ThisStringValue takes argument value (an
- If value
is a String , return value. - If value
is an Object and value has a [[StringData]] internal slot, then- Let s be value.[[StringData]].
Assert : sis a String .- Return s.
- Throw a
TypeError exception.
22.1.3.36 String.prototype [ @@iterator ] ( )
This method returns an Iterator object (
It performs the following steps when called:
- Let O be ?
RequireObjectCoercible (this value). - Let s be ?
ToString (O). - Let closure be a new
Abstract Closure with no parameters that captures s and performs the following steps when called:- Let len be the length of s.
- Let position be 0.
- Repeat, while position < len,
- Let cp be
CodePointAt (s, position). - Let nextIndex be position + cp.[[CodeUnitCount]].
- Let resultString be the
substring of s from position to nextIndex. - Set position to nextIndex.
- Perform ?
GeneratorYield (CreateIterResultObject (resultString,false )).
- Let cp be
- Return
undefined .
- Return
CreateIteratorFromClosure (closure,"%StringIteratorPrototype%" ,%StringIteratorPrototype% ).
The value of the
22.1.4 Properties of String Instances
String instances are
String instances have a
22.1.4.1 length
The number of elements in the String value represented by this String object.
Once a String object is initialized, this property is unchanging. It has the attributes { [[Writable]]:
22.1.5 String Iterator Objects
A String Iterator is an object, that represents a specific iteration over some specific String instance object. There is not a named
22.1.5.1 The %StringIteratorPrototype% Object
The %StringIteratorPrototype% object:
- has properties that are inherited by all String Iterator Objects.
- is an
ordinary object . - has a [[Prototype]] internal slot whose value is
%IteratorPrototype% . - has the following properties:
22.1.5.1.1 %StringIteratorPrototype%.next ( )
- Return ?
GeneratorResume (this value,empty ,"%StringIteratorPrototype%" ).
22.1.5.1.2 %StringIteratorPrototype% [ @@toStringTag ]
The initial value of the
This property has the attributes { [[Writable]]: