Did this ever happened to you that you are not able to remember any string method? Yes this sound silly, but every other beginner suffers from this problem. So here I collected all important string methods at one place. So you can compare all methods and use best suited method in your code. Just bookmark this blog and you will never face such a problem and save your time.
Getting Started
A String is nothing but a sequence of characters.
1. Uses of String
in JavaScript
Some of the basic operations which we can perform on a string are calculating its length
, we can combine/concatenate
multiple strings, get the index value of a particular string, and many other things.
2. Creating a string
There are two ways through which we can create String
in JavaScript.
- Using Variables
- Using
String()
Constructor
Using Variables
const str = 'hello, world!'
console.log(str);
//Output
hello, world!
Using String()
Constructor
const newStr = new String("hello world!")
console.log(newStr);
//Output
hello, world!
3. String
Instance Methods & Property
1. String length
The length
property of String
is used to calculate the number of characters in a particular string. It's a read-only property and it checks in UTF-16 code units.
const checkLength = 'hello world!';
console.log(checkLength.length)
//Output
12
Important Points to remember
- For empty string, the length is zero
- As it is read-only changing the length won't affect anything
let str = "hello world!"
str.length = 10; //No Effect
console.log(str)
//Output
12
2. charAt()
The charAt()
method returns a new string containing the particular character provided by the program.
const str = "hello, world!"
console.log(str.charAt(1))
//Output
e
Points to remember
- If the index is not provided it takes the first character of string, if the index exceeds the range an empty string is displayed
- To get the last character of
string
we can usestr.length-1
- It returns a new
string
with the specified indexed character
3. concat()
The concat()
method of string is used to combine n number of strings together
const str = "hello"
const str2 = "world!"
console.log(str.concat(str2)
//Output
"hello world!"
Points to remember:
concat()
returns a new string- If no arguments are passed it is left as it is.
4. includes()
The includes()
method performs a search on provided string and determines whether the one string is available in other string or not, and on based on that it returns a boolean
results.
const str = "hello, world!"
const str2 = "hello"
const newStr = str.includes(str2)
//Output
true
Points to remember
- It is case - sensitive
- It returns Boolean type.
5. indexOf()
The indexOf()
method returns the value where the indexed value is matched, it matches the first occurrence of the desired string.
const str = "hello world!"
const str2 = "world"
console.log(str.indexOf(str2)
//Output
7
6. repeat()
The repeat()
method returns a new string that contains the specified number of copies of the string on which it was called
const str = "hello "
console.log("Suyash",str.repeat(2));
//Ouput
Suyash hello hello
Points to remember
- Range should not be negative
- If range is negative or more than negative, it throws
RangeError
7. slice()
The slice()
method extracts a section of string and returns a new string, without modifying it.
**Parameters**
slice(beginIndex)
slice(beginIndex,endIndex)
Points to remembers
- beginIndex: It is zero-based, if a negative value is provided it is treated as
string.length - index
, if the value is greater than or equal tostring.length
an empty string is returned. - endIndex: Till which the extraction to be executed, if no value is provided it slices the entire string, if the value exceeds the length of string it slices the entire string as well, if the value is negative it starts with
str.length - indexValue
- It creates a new string always.
8. split()
The split()
methods convert a string into an array and return an array
const str = "hello world"
const strArray = str.split(' ')
console.log(strArray);
//Output
["hello", "world"]
Hope you find this blog useful!