Introduction
In my previous article, I explained the underlying concepts and methods, and properties. In this article, we learned about String objects and String methods and Properties in JavaScript.
Definition
A String is a collection of characters (or) sequence of characters, which is always enclosed with a single (‘’) or double quote (“”). A JavaScript string stores a series of
characters like “c sharp corner”.
Uses of string
-
It is used to store single characters, and
-
It stores an array of characters
-
It is used to manipulate text – search a particular letter and replace text
We can see one small example:
Example
Try it yourself,
Output
![String Objects In JavaScript]()
Note
It is immutable, the string cannot be changed in JavaScript. It is impossible to change a particular character.
Creation of string
-
By string literal
zero or more characters written inside single or double quotes or backticks. The string indexes are zero-based, the first character is in position 0 and the second in 1.
-
By string object using ‘new’ keyword
In this, a new keyword is used to create an instance of a string. The string object has some disadvantages, for example, the execution speed is slow.
Now, we can see an example of string creation.
String as Literals
Try it yourself,
Output
![String Objects In JavaScript]()
String as Object
JavaScript String Object is a universal object that is used to store strings. A string is a combination of letters, numbers, special characters (single or double quotations), and arithmetic values.
Try it yourself,
Output
![String Objects In JavaScript]()
String Object Properties and Methods
String property
String Methods
String Methods |
Description |
1.charAt() |
It returns a character at the specified position. |
2.charCodeAt () |
It returns the Unicode of a specific character. |
3.concat () |
Concatenation joining one or more strings. |
4.indexOf () |
The given string or word to find the position of the specified value in a string. |
5.lastindexOf () |
To find the last position of the specified value in a string. |
6.LocaleCompare () |
Comparing the two words, which is equal means returns 1; it is greater means returns 0, and lesser means returns -1. |
7.Match () |
The given string matches the sentence means and returns the position. |
8.Replace () |
The specified string will be converted to a new string. |
9.Search () |
Search the specific value or string in a given paragraph, and return the position of the match. |
10.Slice () |
The given particular index in the index is indexed with a particular index and returns the position of the match. |
11.Split () |
It will split a string into an array of substrings. |
12.Substr () |
Returns the characters in a string starting at the specified location by a specified number of characters. |
13.subString () |
It returns characters from a string between two specified indexes. |
14.tostring () |
Returns the value of the specified string object. |
15.Trim () |
Eliminates the space between the two ends of a string. |
16.toUpperCase () |
It converts a string to uppercase (capitalize) letters. |
17.toLowerCase () |
It converts a string to Lowercase letters. |
18.valueOf () |
it will display the total string value or primitive value of a string object. |
Example of string Property
Try it yourself,
Output
![String Objects In JavaScript]()
Example of string Methods
In the below example, all the string methods are included in this example.
To Extract String Characters
Try it yourself,
Output
![String Objects In JavaScript]()
To Look for a Substring
Try it yourself,
Output
![String Objects In JavaScript]()
To Compare a String
Try it yourself,
Output
![String Objects In JavaScript]()
To Get a Substring
Try it yourself,
Output
![String Objects In JavaScript]()
To Change Cases
Try it yourself,
Output
![String Objects In JavaScript]()
To remove white space on both sides
Try it yourself,
Output
![String Objects In JavaScript]()
Summary
In this article, we have learned about String Objects, String object Methods, and Properties. I hope this article was helpful to you. Thanks for reading!