JavaScripture
Contribute Feedback

JavaScript String : Object

Strings contain text. They are represented as a sequence of 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.

Example:

Run

Results:

 

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

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

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) : Number

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.

Example:

Run

Results:

 

replace 4 variants
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, 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, 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:

 

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:

 

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. Similar to 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:

 

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 start is less than 0, it is replaced with 0. If end is negative, this.length is added to it before performing the substring. If end is not specified, this.length is used instead. Similar to 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.

Example:

Run

Results:

 

String Methods

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

Returns a new string composed of characters from the specified unicode values. Use charCodeAt() to retrieve character codes from strings.

Example:

Run

Results: