DEV Community

Sarman Khurshid Alam
Sarman Khurshid Alam

Posted on

10 methods of string you need to know

                   charAt() method
Enter fullscreen mode Exit fullscreen mode

syntax: string.charAt(index). This method returns a new string consisting of a single character located at the index's position. For example,
let sentence="I love my country";
console.log(sentence.charAt(0)) prints I because I is located at 0th position. But if the index is bigger than the string size the method will return an empty string.

                  includes() method
Enter fullscreen mode Exit fullscreen mode

syntax: string.includes(subString). This method performs case sensitive search to find whether a substring is in the string.
For example,
const string= "My name is Khan"
const subString="Name"
console.log(string.includes(subString)) prints false because "Name " is not present in the original string if the substring is "name" then it will print true;

                    concat() method
Enter fullscreen mode Exit fullscreen mode

syntax: string.concat(str1,str2). this method concatenates argument string/strings to the calling string. The argument of this method will be one or more string.
For example,
string="How beautiful the bird is";
console.log(string.concat("!")) prints How beautiful the bird is!

                   endsWith() method
Enter fullscreen mode Exit fullscreen mode

syntax: string.ednsWith(subString,length) or string.endsWith(subString) returns true of false depending on whether the string ends with subString within the given length. If the length is not given the default length will be the string length.
For example,
let string="Cats are the best!"
let subString ="best!"
console.log(string.endsWith(subString)); prints true but
console.log(string.endsWith(subString,17)) prints false because within length of 17 the string ends with best.If we write
console.log(string.endsWith("best",17) then it will print true;

                indexOf() method
Enter fullscreen mode Exit fullscreen mode

syntax: string.indexOf(subString)
string.indexOf(subString,startingIndex)
returns the first occurrence of the subString within the string and it returns -1 if the subString is not found in the string.
For example,
let string = "I love my country"
let subString= "love"
console.log(string.indexOf(subString)) prints 2 because the first letter of love is at index 2 and
console.log(string.indexOf(subString,3)) prints -1 because after index 3 there is no substring of "love"

                lastIndexOf() method
Enter fullscreen mode Exit fullscreen mode

syntax: string.lastIndexOf(subString) returns the last occurrence of the substring in the string.
For example,
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';

console.log(The index of the first "${searchTerm}" from the end is ${paragraph.lastIndexOf(searchTerm)});
// expected output: "The index of the first "dog" from the end is 52"

                slice() method
Enter fullscreen mode Exit fullscreen mode

syntax: string.slice(index)
this method returns a new string from the given position without modifying the original string.
For example,
let string="Bangladesh is a beautiful country"
console.log(string.slice(1)) prints angladesh is a beautiful country.

                       split() method
Enter fullscreen mode Exit fullscreen mode

syntax: string.split(patternString)
this method divides the string into substring based on the pattern string put these substrings into an array and return these array.
For example,
string ="i love my country"
let array=string.split(' ');
console.log(array[0]) prints I

                    toLowerCase() method
Enter fullscreen mode Exit fullscreen mode

this method returns a new string where all the uppercase letters converted to lowercase without modifying the original string.
For example,
let string ="I Love My Country"
console.log(string.toLowerCase());
prints i love my country.

                  toUpperCase() method
Enter fullscreen mode Exit fullscreen mode

this method returns a new string where all the lowercase letters converted to uppercase without modifying the original string.
For example,
let string ="sarman"
console.log(string.toUpperCase());
prints SARMAN

Top comments (0)