JavaScripture
Contribute via GitHub Feedback

String : Object

Strings contain text. They are represented as a sequence of UTF-16 characters. JavaScript strings are immutable.

String(value : Object) : String

Returns a string representation of value by calling value.toString().

Example:

Run

Results:

 

Constructors

new String(value : String) : Object

Creates a box for a string value.

Example:

Run

Results:

 

Instance Properties

length : Number  

The length property is the number of characters in the string. It is 1 greater than the index of the last character.

Example:

Run

Results:

 

Instance Methods

charAt(pos : Number) : String

Returns a String of length 1 that contains the character at position pos. Another way to retrieve a character in a string is this[pos].

Example:

Run

Results:

 

charCodeAt(pos : Number) : Number

Returns the unicode value of the character at position pos. Use String.fromCharCode() to generate a string from character code values. See also TextEncoder.

Example:

Run

Results:

 

codePointAt(pos : Number) : Number

Returns the UTF-32 code point of starting at pos. If the UTF-16 value at pos does not start a surrogate pair, the returned value will be the UTF-16 value at pos. Use String.fromCodePoint() to generate a string from code points. See also TextEncoder.

Example:

Run

Results:

 

concat(string0 : String, [string1 : String, [...]]) : String

Returns a new String formed by joining each of the string parameters.

Example:

Run

Results:

 

endsWith(searchString : String, [endIndex : Number]) : Boolean

Returns true if searchString is at the end of this. If endIndex is specified, the last character of searchString must be at endIndex - 1 in this.

Example:

Run

Results:

 

includes(searchString : String, [startingIndex = 0 : Number]) : Boolean

Returns true if searchString is somewhere in this starting the search from startingIndex. If startingIndex is negative, this.length is added to it before starting the search.

Example:

Run

Results:

 

indexOf(searchString : String, [startingIndex = 0 : Number]) : Number

Returns the first location of searchString in this starting the search from startingIndex. Returns -1 if searchString is not found.

Example:

Run

Results:

 

lastIndexOf(searchString : String, [startingIndex : Number]) : Number

Returns the location of searchString in this starting the search from startingIndex by searching backwards through the string. If startingIndex is not specified, the search starts from the end of the string. Returns -1 if searchString is not found.

Example:

Run

Results:

 

localeCompare(that : String, [locales : Array<String>, [options : Object]]) : Number
options : {
caseFirst :StringMust be one of 'lower', 'upper', or 'false'.
ignorePunctuation :Boolean
localeMatcher :StringMust be one of 'best fit' or 'lookup'.
numeric :Boolean
sensitivity :StringMust be one of 'accent', 'base', 'case', or 'variant'.
usage :StringMust be one of 'search' or 'sort'.
}

Compares this to that. Returns a negative number if this would sort before that, 0 if this and that are equal, and a positive number if this would sort after that.

Example:

Run

Results:

 

match(regexp : RegExp) : Array<String>

If regexp matches this, returns a new Array with item 0 equal to the portion of this that matched the regular expression, item 1 equal to the first capturing group in regexp, item 2 equal to the second capturing group in regexp, and so on. The returned Array will also have an index property set to the starting index of the match and an input property set to this. If regexp doesn't match this, returns null. See also String.matchAll() and RegExp.exec().

Example:

Run

Results:

 

You can name capture groups by placing ?<name> at the start of the group. The captured values are available on a groups property on the returned array. Note, this is new with ECMAScript 2018.

Example:

Run

Results:

 

matchAll(regexp : RegExp) : Iterator<Array<String>>

Returns an iterator of all places regexp matches this. regexp must be a global RegExp.

Example:

Run

Results:

 

normalize(form : String) : String

Returns a normalized form of this according to form. form must be one of 'NFC', 'NFD', 'NFKC', or 'NFKD'. See https://unicode.org/reports/tr15/.

Example:

Run

Results:

 

padEnd(minLength : Number, [fill = ' ' : String]) : String

Returns a new string with fill added to the end of this until length is equal to minLength. If length is already greater than or equal to minLength, returns this unchanged.

Example:

Run

Results:

 

padStart(minLength : Number, [fill = ' ' : String]) : String

Returns a new string with fill added to the start of this until length is equal to minLength. If length is already greater than or equal to minLength, returns this unchanged.

Example:

Run

Results:

 

repeat(count : Number) : String

Returns a new String formed by repeating this count times.

Example:

Run

Results:

 

replace 4 variants
replace(searchValue : String, replaceFunction : Function) : String
replaceFunction(match : String, offset : Number, string : String) : String

Returns a new String where the first occurrence of searchValue in this is replaced with the value returned from calling replaceFunction.

The match parameter to replaceFunction is the same as searchValue, offset is the index in this where searchValue was found, and string is this.

Example:

Run

Results:

 

replace(searchValue : RegExp, replaceFunction : Function) : String
replaceFunction(match : String, capture1 : String, capture2 : String, ..., offset : Number, string : String) : String

Returns a new String where searchValue matches in this is replaced with the value returned from calling replaceFunction. If searchValue is a global RegExp, each match in this will be replaced. Otherwise, just the first match will be replaced.

The match parameter to replaceFunction is the same as searchValue, the capture parameters are the values of the capture groups in searchValue (if any), offset is the index in this where searchValue was found, and string is this.

Example:

Run

Results:

 

replace(searchValue : String, replaceValue : String) : String

Returns a new String where the first occurrence of searchValue in this is replaced with replaceValue.

Example:

Run

Results:

 

replace(searchValue : RegExp, replaceValue : String) : String

Returns a new String where searchValue is replaced with replaceValue. If searchValue is a global RegExp, each match in this will be replaced. Otherwise, just the first match will be replaced.

Example:

Run

Results:

 

replaceAll 4 variants
replaceAll(searchValue : String, replaceFunction : Function) : String
replaceFunction(match : String, offset : Number, string : String) : String

Returns a new String where all occurrences of searchValue in this are replaced with the value returned from calling replaceFunction.

The match parameter to replaceFunction is the same as searchValue, offset is the index in this where searchValue was found, and string is this.

Example:

Run

Results:

 

replaceAll(searchValue : RegExp, replaceFunction : Function) : String
replaceFunction(match : String, capture1 : String, capture2 : String, ..., offset : Number, string : String) : String

Returns a new String where all matches of searchValue in this are replaced with the value returned from calling replaceFunction. searchValue must be a global RegExp.

The match parameter to replaceFunction is the same as searchValue, the capture parameters are the values of the capture groups in searchValue (if any), offset is the index in this where searchValue was found, and string is this.

Example:

Run

Results:

 

replaceAll(searchValue : String, replaceValue : String) : String

Returns a new String where all occurrences of searchValue in this are replaced with replaceValue.

Example:

Run

Results:

 

replaceAll(searchValue : RegExp, replaceValue : String) : String

Returns a new String where all matches of searchValue are replaced with replaceValue. searchValue must be a global RegExp.

Example:

Run

Results:

 

search(regexp : RegExp) : Number

Executes regexp on this and returns the index of the first match. Returns -1 if regexp does not match this.

Example:

Run

Results:

 

slice(startIndex : Number, [endIndex : Number]) : String

Returns a new string composed of the section of this between start and end-1. If endIndex is not specified, this.length is used instead. If startIndex or endIndex is negative, this.length is added to it before performing the substring. See also substring().

Example:

Run

Results:

 

split 2 variants
split(separator : String, [limit : Number]) : Array<String>

Splits this at separator into an Array of Strings. If limit is specified, the returned array will contain no more than limit items.

Example:

Run

Results:

 

split(separator : RegExp, [limit : Number]) : Array<String>

Splits this into an Array of Strings. If limit is specified, the returned array will contain no more than limit items.

Example:

Run

Results:

 

startsWith(searchString : String, [startingIndex = 0 : Number]) : Boolean

Returns true if searchString is at the beginning of this. If startingIndex is specified, searchString must be at startingIndex in this.

Example:

Run

Results:

 

substring(start : Number, [end : Number]) : String

Returns a new string composed of the section of this between start and end-1. Before performing the operation, substring may modifiy the effective values of start and end as follows. If both start and end are specified, and end is less than start, the values are swapped. If either start or end is less than 0, it is replaced with 0. If end is not specified, this.length is used instead. See also slice().

Example:

Run

Results:

 

toLocaleLowerCase() : String

Returns a copy of this where each character is lower case.

Example:

Run

Results:

 

toLocaleUpperCase() : String

Returns a copy of this where each character is upper case.

Example:

Run

Results:

 

toLowerCase() : String

Returns a copy of this where each character is lower case.

Example:

Run

Results:

 

toUpperCase() : String

Returns a copy of this where each character is upper case.

Example:

Run

Results:

 

trim() : String

Returns a copy of this with leading and trailing whitespace removed. See also trimStart() and trimEnd().

Example:

Run

Results:

 

trimEnd() : String

Returns a copy of this with trailing whitespace removed. See also trim() and trimStart().

Example:

Run

Results:

 

trimStart() : String

Returns a copy of this with leading whitespace removed. See also trim() and trimEnd().

Example:

Run

Results:

 

String Methods

fromCharCode(char0 : Number, [char1 : Number, [...]]) : String

Returns a new string composed of characters from the specified UTF-16 values. Each code should be between 0 and 0xFFFF inclusive. Use charCodeAt() to retrieve character codes from strings. See also fromCodePoint() and TextDecoder.

Example:

Run

Results:

 

fromCodePoint(codePoint0 : Number, [codePoint1 : Number, [...]]) : String

Returns a new string composed of characters from the specified UTF-32 code points. Each code point should be between 0 and 0x10FFFF inclusive. Use codePointAt() to retrievecodes from strings. See also fromCharCode() and TextDecoder.

Example:

Run

Results:

 

raw(template : StringTemplateArray, [sub1 : String, [sub2 : String, [...]]]) : String

String.raw is intended to be used as a tagged templated string. It returns the string as typed in JavaScript, without applying character escaping. For example, raw`foo\nbar` returns foo\nbar, with no new line in the string.

Example:

Run

Results: