JavaScript - The Strings Object
JavaScript - The Strings Object
The String object in JavaScript lets you work with a series of characters; it wraps JavaScript's 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.
The string is a sequence of characters containing 0 or more characters. For example, 'Hello' is a string.
Syntax
JavaScript strings can be created as objects using the String() constructor or as primitives using string literals.
The String parameter, value is a series of characters that has been properly encoded.
We can create string primitives using string literals and the String() function as follows −
String Properties
Here is a list of the properties of String object and their description.
constructor
1
Returns a reference to the String function that created the object.
length
2
Returns the length of the string.
prototype
3
The prototype 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.
Static Methods
The static methods are invoked using the 'String' class itself.
fromCharCode()
1
Converts the sequence of UTF-16 code units into the string.
fromCodePoint()
2
Creates a string from the given sequence of ASCII values.
Instance Methods
The instance methods are invoked using the instance of the String class.
at()
1
Returns the character from the specified index.
charAt()
2
Returns the character at the specified index.
charCodeAt()
3
Returns a number indicating the Unicode value of the character at the given index.
codePointAt()
4
Returns a number indicating the Unicode value of the character at the given index.
concat()
5
Combines the text of two strings and returns a new string.
endsWith()
6
Checks whether the string ends with a specific character or substring.
includes()
7
To check whether one string exists in another string.
8 indexOf()
Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not
found.
lastIndexOf()
9 Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not
found.
localeCompare()
10 Returns a number indicating whether a reference string comes before or after or is the same as the given
string in sort order.
match()
11
Used to match a regular expression against a string.
matchAll()
12
Used to match all occurrences of regular expression patterns in the string.
normalize()
13
To get the Unicode normalization of the string.
padEnd()
14
To add padding to the current string with different strings at the end.
padStart()
15
To add padding to the current string with different strings at the start.
replace()
16 Used to find a match between a regular expression and a string and replace the matched substring with a
new one.
repalceAll()
17 Used to find a match between a regular expression and a string and replace all the matched substring with a
new one.
repeat()
18
To get a new string containing the N number of copies of the current string.
search()
19
Executes the search for a match between a regular expression and a specified string.
slice()
20
Extracts a section of a string and returns a new string.
split()
21
Splits a String object into an array of strings by separating the string into substrings.