ES6 - Strings



The String object lets you work with a series of characters; it wraps JavaScripts string primitive data type with a number of helper methods.

As JavaScript automatically converts between string primitives and String objects, you can call any of the helper methods of the String object on a string primitive.

Use the following syntax to create a String object.

var val = new String(string);

The string parameter is a series of characters that has been properly encoded. String.

String Properties

Following is a list of the properties of String object and its description.

Sr.NoProperty & Description
1constructor

Returns a reference to the String function that created the object .

2length

Returns the length of the string.

3

The property allows you to add properties and methods to an object .

String Methods

Here is a list of the methods available in String object along with their description.

Sr.NoMethod & Description
1charAt()

Returns the character at the specified index.

2charCodeAt()

Returns a number indicating the Unicode value of the character at the given index.

3concat()

Combines the text of two strings and returns a new string.

4indexOf()

Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.

5lastIndexOf()

Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.

6localeCompare()

Returns a number indicating whether a reference string comes before or after or is the same as the given string in a sorted order.

7match()

Used to match a regular expression against a string.

8replace()

Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.

9search()

Executes the search for a match between a regular expression and a specified string.

10slice()

Extracts a section of a string and returns a new string.

11split()

Splits a String object into an array of strings by separating the string into substrings.

12substr()

Returns the characters in a string beginning at the specified location through the specified number of characters.

13substring()

Returns the characters in a string between two indexes into the string.

14toLocaleLowerCase()

The characters within a string are converted to lower case while respecting the current locale.

15toLocaleupperCase()

The characters within a string are converted to uppercase while respecting the current locale.

16toLowerCase()

Returns the calling string value converted to lowercase.

17toString()

Returns a string representing the specified object.

18toUpperCase()

Returns the calling string value converted to uppercase.

19valueOf()

Returns the primitive value of the specified object.