Class String

The String object represents any sequence of zero or more characters that are to be treated strictly as text.

PropertyDescription
length: NumberThe length of the String object.
ConstructorDescription
String()Constructs the String.
String(Number)Constructs the String from the specified Number object.
String(String)Constructs a new String from the specified String object.
MethodDescription
charAt(Number)Returns a string containing the character at position index.
charCodeAt(Number)Returns the UTF-16 code unit at the given position index.
codePointAt(Number)Returns the Unicode code point at the given position index.
concat(String...)Returns a new String created by concatenating the string arguments together.
endsWith(String)Tests if this string ends with a given string.
endsWith(String, Number)Tests if this string ends with a given string.
equals(Object)Returns true if this string is equal to the string representation of the passed objects.
equalsIgnoreCase(Object)Returns true if this string is equal to the string representation of the passed objects.
static fromCharCode(Number...)Returns a new String from one or more UTF-16 code units.
static fromCodePoint(Number...)Returns a new String from one or more characters with Unicode code points.
includes(String)Returns if substring is contained in this String object.
includes(String, Number)Returns if substring is contained in this String object.
indexOf(String)Returns the index of substring in this String object.
indexOf(String, Number)Returns the index of substring in this String object using the specified start value as the location to begin searching.
lastIndexOf(String)Returns the last index of substring in this String object.
lastIndexOf(String, Number)Returns the last index of substring in this String object, using the specified start position as the location from which to begin the search.
localeCompare(String)Returns a number indicating whether the current String sorts before, the same as, or after the parameter other, based on browser and system-dependent string localization.
match(RegExp)Returns an array of strings that match the regular expression regexp.
normalize()Returns the normalized form of this Unicode string.
normalize(String)Returns the normalized form of this Unicode string according to the standard as described in https://unicode.org/reports/tr15/.
padEnd(Number)Appends space characters to the current string to ensure the resulting string reaches the given target length.
padEnd(Number, String)Appends a string (possibly multiple times) to the current string to ensure the resulting string reaches the given target length.
padStart(Number)Prepends space characters to the current string to ensure the resulting string reaches the given target length.
padStart(Number, String)Prepends a string (possibly multiple times) to the current string to ensure the resulting string reaches the given target length.
static raw(Object, String...)The static String.raw() method is a tag function of template literals.
repeat(Number)Returns a new string repeating this string the given number of times.
replace(RegExp, Function)Returns a new String that results when matches of the regexp parameter are replaced by using the specified function.
replace(RegExp, String)Returns a new String that results when matches of the regexp parameter are replaced by the replacement parameter.
replace(String, Function)Returns a new String that results when matches of the literal parameter are replaced by using the specified function.
replace(String, Function, String)Returns a new String that results when matches of the literal parameter are replaced by using the specified function.
replace(String, String)Returns a new String that results when matches of the literal parameter are replaced by the replacement parameter.
replace(String, String, String)Returns a new String that results when matches of the literal parameter are replaced by the replacement parameter.
search(RegExp)Searches for a match between the passed regular expression and this string and returns the zero-based index of the match, or -1 if no match is found.
slice(Number, Number)Returns a substring of the current String where the specified start and end locations are used to delimit the String.
split(RegExp)Returns an array of String instances created by splitting the current String based on the regular expression.
split(RegExp, Number)Returns an array of String instances created by splitting the current String based on the regular expression and limited in size by the limit parameter.
split(String)Returns an array of String instances created by splitting the current String based on the delimiter.
split(String, Number)Returns an array of String instances created by splitting the current String based on the delimiter and limited in size by the limit parameter.
startsWith(String)Tests if this string starts with a given string.
startsWith(String, Number)Tests if this string starts with a given string.
substr(Number)Creates and returns a new String by splitting the current string at the specified start location until the end of the String.
substr(Number, Number)Creates and returns a new String by splitting the current string at the specified start location and limited by the length value.
substring(Number)Creates and returns a new String by splitting the current string at the specified from location until the end of the String.
substring(Number, Number)Creates and returns a new String by splitting the current string at the specified from location until the specified to location.
toLocaleLowerCase()Returns a copy of the current string in all lower-case letters.
toLocaleUpperCase()Returns a copy of the current string in all upper-case letters.
toLowerCase()Returns a copy of the current string in all lower-case letters.
toString()Returns a String value of this object.
toUpperCase()Returns a copy of the current string in all upper-case letters.
trim()Removes white space characters at the start and the end of the string.
trimEnd()Removes white space characters at the end of the string.
trimLeft()Removes white space characters at the start of the string.
trimRight()Removes white space characters at the end of the string.
trimStart()Removes white space characters at the start of the string.
valueOf()Returns a String value of this object.

assign, create, create, defineProperties, defineProperty, entries, freeze, fromEntries, getOwnPropertyDescriptor, getOwnPropertyNames, getOwnPropertySymbols, getPrototypeOf, hasOwnProperty, is, isExtensible, isFrozen, isPrototypeOf, isSealed, keys, preventExtensions, propertyIsEnumerable, seal, setPrototypeOf, toLocaleString, toString, valueOf, values

length: Number

The length of the String object.


String()

Constructs the String.


String(num: Number)

Constructs the String from the specified Number object.

Parameters:

  • num - the number that will be converted to a String.

String(str: String)

Constructs a new String from the specified String object.

Parameters:

  • str - the String that will be converted to a new String.

charAt(index: Number): String

Returns a string containing the character at position index. You should use this method instead of substring when you need only a single character.

Parameters:

  • index - the index at which the character string is located.

Returns:

  • a string containing the character at position index.

charCodeAt(index: Number): Number

Returns the UTF-16 code unit at the given position index. If the position is invalid NaN is returned.

Parameters:

  • index - The index of the code unit within the string.

Returns:

  • a non-negative integer representing a UTF-16 code unit or NaN if the index is not valid.

codePointAt(index: Number): Number

Returns the Unicode code point at the given position index. The index is a position within the string in UTF-16 encoding. If the index points to the begin of a surrogate pair the only the code unit at the position is returned. If the index is invalid undefined is returned.

Parameters:

  • index - The index of the starting code unit within the string.

Returns:

  • The Unicode code point, an UTF-16 code unit or undefined.

API Version:

Available from version 21.2.


concat(strings: String...): String

Returns a new String created by concatenating the string arguments together.

Parameters:

  • strings - zero, one, or more String arguments

Returns:

  • a new String created by concatenating the string arguments together.

endsWith(searchString: String): Boolean

Tests if this string ends with a given string.

Parameters:

  • searchString - The characters to be searched for this string.

Returns:

  • true if the search string was found else false

API Version:

Available from version 21.2.


endsWith(searchString: String, length: Number): Boolean

Tests if this string ends with a given string.

Parameters:

  • searchString - The characters to be searched for this string.
  • length - Assumes this string has only this given length.

Returns:

  • true if the search string was found else false

API Version:

Available from version 21.2.


equals(obj: Object): Boolean

Returns true if this string is equal to the string representation of the passed objects.

Parameters:

  • obj - another object, typically another string

equalsIgnoreCase(obj: Object): Boolean

Returns true if this string is equal to the string representation of the passed objects. The comparison is done case insensitive.

Parameters:

  • obj - another object, typically another string

static fromCharCode(c: Number...): String

Returns a new String from one or more UTF-16 code units.

Parameters:

  • c - zero, one, or more UTF-16 code units.

static fromCodePoint(c: Number...): String

Returns a new String from one or more characters with Unicode code points.

Parameters:

  • c - zero, one, or more code points.

API Version:

Available from version 21.2.


includes(substring: String): Boolean

Returns if substring is contained in this String object.

Parameters:

  • substring - the String to search for in this String.

Returns:

  • true if substring occurs within the current string, else false.

API Version:

Available from version 21.2.


includes(substring: String, start: Number): Boolean

Returns if substring is contained in this String object.

Parameters:

  • substring - the String to search for in this String.
  • start - the location in the String from which to begin the search.

Returns:

  • true if substring occurs within the current string, else false.

API Version:

Available from version 21.2.


indexOf(substring: String): Number

Returns the index of substring in this String object. If there is no match, -1 is returned.

Parameters:

  • substring - the String to search for in this String.

Returns:

  • the index of substring or -1.

indexOf(substring: String, start: Number): Number

Returns the index of substring in this String object using the specified start value as the location to begin searching. If there is no match, -1 is returned.

Parameters:

  • substring - the String to search for in this String.
  • start - the location in the String from which to begin the search.

Returns:

  • the index of substring or -1.

lastIndexOf(substring: String): Number

Returns the last index of substring in this String object. If there is no match, -1 is returned.

Parameters:

  • substring - the String to search for in this String.

Returns:

  • the last index of substring or -1.

lastIndexOf(substring: String, start: Number): Number

Returns the last index of substring in this String object, using the specified start position as the location from which to begin the search. If there is no match, -1 is returned.

Parameters:

  • substring - the String to search for in this String.
  • start - the location from which to begin the search.

Returns:

  • the last index of substring or -1.

localeCompare(other: String): Number

Returns a number indicating whether the current String sorts before, the same as, or after the parameter other, based on browser and system-dependent string localization.

Parameters:

  • other - the String to compare against this String.

Returns:

  • a number indicating whether the current String sorts before, the same as, or after the parameter other.

match(regexp: RegExp): String[]

Returns an array of strings that match the regular expression regexp.

Parameters:

  • regexp - the regular expression to use.

Returns:

  • an array of strings that match the regular expression.

normalize(): String

Returns the normalized form of this Unicode string. Same as calling normalize(String) with parameter 'NFC'.

Returns:

  • The normalized form of this Unicode string.

API Version:

Available from version 21.2.


normalize(form: String): String

Returns the normalized form of this Unicode string according to the standard as described in https://unicode.org/reports/tr15/. In a normalized string, identical text is replaced by identical sequences of code points.

Parameters:

  • form - The normalization variant to use. Must be one of 'NFC', 'NFD', 'NFKC', 'NFKD'.

Returns:

  • The normalized form of this Unicode string.

API Version:

Available from version 21.2.


padEnd(targetLength: Number): String

Appends space characters to the current string to ensure the resulting string reaches the given target length.

Parameters:

  • targetLength - The length to be reached.

Returns:

  • This string if the string length is already greater than or equal to the target length. Else a new string with the targetLength ending with space characters.

API Version:

Available from version 21.2.


padEnd(targetLength: Number, fillString: String): String

Appends a string (possibly multiple times) to the current string to ensure the resulting string reaches the given target length.

Parameters:

  • targetLength - The length to be reached.
  • fillString - The string providing the characters to be used for filling.

Returns:

  • This string if the string length is already greater than or equal to the target length. Else a new string with the targetLength with the (possibly multiple times) added and truncated fillString.

API Version:

Available from version 21.2.


padStart(targetLength: Number): String

Prepends space characters to the current string to ensure the resulting string reaches the given target length.

Parameters:

  • targetLength - The length to be reached.

Returns:

  • This string if the string length is already greater than or equal to the target length. Else a new string with the targetLength starting with space characters.

API Version:

Available from version 21.2.


padStart(targetLength: Number, fillString: String): String

Prepends a string (possibly multiple times) to the current string to ensure the resulting string reaches the given target length.

Parameters:

  • targetLength - The length to be reached.
  • fillString - The string providing the characters to be used for filling.

Returns:

  • This string if the string length is already greater than or equal to the target length. Else a new string with the targetLength with the (possibly multiple times) added and truncated fillString.

API Version:

Available from version 21.2.


static raw(callSite: Object, substitutions: String...): String

The static String.raw() method is a tag function of template literals.

It can be used in different ways:

Parameters:

  • callSite - A well-formed template call site object, like { raw: ['a', 'b', 'c'] }.
  • substitutions - The substitution values.

Returns:

  • A string constructed by the template with filled substitutions.

API Version:

Available from version 22.7.


repeat(count: Number): String

Returns a new string repeating this string the given number of times.

Parameters:

  • count - The number of times this string should be repeated. Must be non-negative.

Returns:

  • A new string repeating this string the given number of times.

API Version:

Available from version 21.2.


replace(regexp: RegExp, function: Function): String

Returns a new String that results when matches of the regexp parameter are replaced by using the specified function. The original String is not modified so you must capture the new String in a variable to preserve changes. When you specify a function as the second parameter, the function is invoked after the match has been performed.

Parameters:

  • regexp - the regular expression to use.
  • function - a Function that operates on matches of regexp in the current String.

Returns:

  • a new String that results when matches of the regexp parameter are replaced by the function.

replace(regexp: RegExp, replacement: String): String

Returns a new String that results when matches of the regexp parameter are replaced by the replacement parameter. The original String is not modified so you must capture the new String in a variable to preserve changes. If regexp has the global flag set, all occurrences are replaced, if the global flag is not set only the first occurrence is replaced.

Parameters:

  • regexp - the regular expression to use.
  • replacement - a String that is to take the place of all matches of regexp in the current String.

Returns:

  • a new String that results when matches of the regexp parameter are replaced by the replacement.

replace(literal: String, function: Function): String

Returns a new String that results when matches of the literal parameter are replaced by using the specified function. The original String is not modified so you must capture the new String in a variable to preserve changes. When you specify a function as the second parameter, the function is invoked after the match has been performed.

Parameters:

  • literal - the literal string to locate.
  • function - a Function that operates on the match of literal in the current String.

Returns:

  • a new String that results when the first match of the literal parameter is replaced by the specified function.

replace(literal: String, function: Function, flags: String): String

Returns a new String that results when matches of the literal parameter are replaced by using the specified function. The original String is not modified so you must capture the new String in a variable to preserve changes. When you specify a function as the second parameter, the function is invoked after the match has been performed.

Parameters:

  • literal - the literal string to locate.
  • function - a Function that operates on the match of literal in the current String.
  • flags - a String containing any combination of the Regular Expression flags of g - global match, i - ignore case, m - match over multiple lines.

Returns:

  • a new String that results when the first match of the literal parameter is replaced by the specified function.

Deprecated:

Use replace(RegExp, Function) instead.

No longer available as of version 21.2.


replace(literal: String, replacement: String): String

Returns a new String that results when matches of the literal parameter are replaced by the replacement parameter. The original String is not modified so you must capture the new String in a variable to preserve changes. This method only replaces the first occurrence of the literal. To replace all occurrences see the polymorphic method with a regular expression as argument.

Parameters:

  • literal - the literal string to locate.
  • replacement - a String that is to take the place of all matches of regexp in the current String.

Returns:

  • a new String that results when the first match of the literal parameter is replaced by the replacement parameter.

replace(literal: String, replacement: String, flags: String): String

Returns a new String that results when matches of the literal parameter are replaced by the replacement parameter. The original String is not modified so you must capture the new String in a variable to preserve changes. This method only replaces the first occurrence of the literal. To replace all occurrences see the polymorphic method with a regular expression as argument. Note that if flags

Parameters:

  • literal - the literal string to locate.
  • replacement - a String that is to take the place of all matches of regexp in the current String.
  • flags - a String containing any combination of the Regular Expression flags of g - global match, i - ignore case, m - match over multiple lines.

Returns:

  • a new String that results when the first match of the literal parameter is replaced by the replacement parameter.

Deprecated:

Use replace(RegExp, String) instead.

No longer available as of version 21.2.


search(regexp: RegExp): Number

Searches for a match between the passed regular expression and this string and returns the zero-based index of the match, or -1 if no match is found.

Parameters:

  • regexp - the regular expression to use.

Returns:

  • the zero-based indexed value of the first character in the current string that matches the pattern of the regular expression regexp, or -1 if no match is found.

slice(start: Number, end: Number): String

Returns a substring of the current String where the specified start and end locations are used to delimit the String.

Parameters:

  • start - the start position in the current String from which the slice will begin.
  • end - the end position in the current String from which the slice will terminate.

Returns:

  • the String between the start and end positions.

split(regexp: RegExp): String[]

Returns an array of String instances created by splitting the current String based on the regular expression.

Parameters:

  • regexp - the regular expression to use to split the string.

Returns:

  • an array of String instances created by splitting the current String based on the regular expression.

split(regexp: RegExp, limit: Number): String[]

Returns an array of String instances created by splitting the current String based on the regular expression and limited in size by the limit parameter.

Parameters:

  • regexp - the regular expression to use to split the string.
  • limit - controls the maximum number of items that will be returned.

Returns:

  • an array of String instances created by splitting the current String based on the regular expression and limited in size by the limit parameter.

split(delimiter: String): String[]

Returns an array of String instances created by splitting the current String based on the delimiter.

Parameters:

  • delimiter - the delimiter to use to split the string.

Returns:

  • an array of String instances created by splitting the current String based on the delimiter.

split(delimiter: String, limit: Number): String[]

Returns an array of String instances created by splitting the current String based on the delimiter and limited in size by the limit parameter.

Parameters:

  • delimiter - the delimiter to use to split the string.
  • limit - controls the maximum number of items that will be returned.

Returns:

  • an array of String instances created by splitting the current String based on the delimiter and limited in size by the limit parameter.

startsWith(searchString: String): Boolean

Tests if this string starts with a given string.

Parameters:

  • searchString - The characters to be searched for this string.

Returns:

  • true if the search string was found else false

API Version:

Available from version 21.2.


startsWith(searchString: String, position: Number): Boolean

Tests if this string starts with a given string.

Parameters:

  • searchString - The characters to be searched for this string.
  • position - The position in this string at which to begin searching for searchString.

Returns:

  • true if the search string was found else false

API Version:

Available from version 21.2.


substr(start: Number): String

Creates and returns a new String by splitting the current string at the specified start location until the end of the String.

Parameters:

  • start - the start position in the current string from which the new string will be created.

Returns:

  • a new String created by splitting the current string starting at the specified start location until the end of the String.

substr(start: Number, length: Number): String

Creates and returns a new String by splitting the current string at the specified start location and limited by the length value.

Parameters:

  • start - the start position in the current string from which the new string will be created.
  • length - controls the length of the new string.

Returns:

  • a new String created by splitting the current string starting at the specified start location and limited by the length value.

substring(from: Number): String

Creates and returns a new String by splitting the current string at the specified from location until the end of the String.

Parameters:

  • from - the start position in the current string from which the new string will be created.

Returns:

  • a new String created by splitting the current string starting at the specified from location until the end of the String.

substring(from: Number, to: Number): String

Creates and returns a new String by splitting the current string at the specified from location until the specified to location.

Parameters:

  • from - the start position in the current string from which the new string will be created.
  • to - the end position in the current string from which the new string will be created.

Returns:

  • a new String created by splitting the current string starting at the specified from location until the specified to location. value.

toLocaleLowerCase(): String

Returns a copy of the current string in all lower-case letters.

Returns:

  • a copy of the current string in all lower-case letters.

toLocaleUpperCase(): String

Returns a copy of the current string in all upper-case letters.

Returns:

  • a copy of the current string in all upper-case letters.

toLowerCase(): String

Returns a copy of the current string in all lower-case letters.

Returns:

  • a copy of the current string in all lower-case letters.

toString(): String

Returns a String value of this object.

Returns:

  • a String value of this object.

toUpperCase(): String

Returns a copy of the current string in all upper-case letters.

Returns:

  • a copy of the current string in all upper-case letters.

trim(): String

Removes white space characters at the start and the end of the string.

Returns:

  • A new string without leading and ending white space characters.

trimEnd(): String

Removes white space characters at the end of the string.

Returns:

  • A new string without ending white space characters.

API Version:

Available from version 21.2.


trimLeft(): String

Removes white space characters at the start of the string. trimStart() should be used instead of this.

Returns:

  • A new string without leading white space characters.

API Version:

Available from version 21.2.


trimRight(): String

Removes white space characters at the end of the string. trimEnd() should be used instead of this.

Returns:

  • A new string without ending white space characters.

API Version:

Available from version 21.2.


trimStart(): String

Removes white space characters at the start of the string.

Returns:

  • A new string without leading white space characters.

API Version:

Available from version 21.2.


valueOf(): String

Returns a String value of this object.

Returns:

  • a String value of this object.