Loading...

String methods in JavaScript

String methods in JavaScript

What is a string?

A string is a primitive datatype in JavaScript which is enclosed in a single quote, double-quote, or a backquote. They are used to represent a set of characters. They are immutable. Even the slightest change in the old string results in a new string and the old string is not changed. Strings in JavaScript are case-sensitive. The same word with lower and upper cases are considered as different strings. Ex: “codedamn” and “CODEDAMN” are two different strings.  Some of the ways in which we can declare a string in JavaScript are shown below.

  • String1 = (“Codedamn”)
  • String2 = new string(“Codedamn”)

A string can represent data or an object. The primitive strings are converted to string objects making it possible to use string methods even for the primitive strings. There are a lot of methods and properties that we use on strings. Let us discuss some of them in detail.

String Methods

String methods are those methods that make our work with strings easier. We will be able to do complex work very easily. Let us discuss some of the string methods and properties in detail. 

1) Finding the length of a string

Sometimes there comes a need of counting the number of letters in the string. At that time we can’t waste our time counting them. We can use the .length property to count the number of letters in the string.

Example:

const string = (“codedamn”);
console.log(string.length);

We get the output of the above-written code as 8 as the number of letters in the word codedamn adds to 8.

2) Returning a specific letter from the string

We use the notation of square brackets to return any character inside the string. Each letter inside the string has a numbering. We include the numbering of the character inside the square bracket to return that particular character. The numbering of a string always starts with 0.

Example:

const string = (“Hello World”);
string[9]

The result of the above-written code is ”d”.

We can also print the last letter of the string as shown below.

string[string.length-1];

We have seen in the 1st case that .length returns the length of the string.

3) Checking substring in a string

We can check if a particular smaller string is present in a string by using the .includes property. It returns true if the string contains that particular smaller string and false if the string doesn’t contain it. Let us understand it with an example.

const string = (“codedamn”)
if (string.includes(“code”)) {
  console.log(“Found!”)
} else {
  console.log(“Not Found!”);
}

We get the output as true as the string codedamn contains the substring code.

To know if the string starts or ends with a particular substring we can use the methods startswith() and endswith(). Let us understand their usage with an example.

const string = (“codedamn”) ;
if (string.startswith(“code”)) {
  console.log(“ YES!! ”) ;
} else {
  console.log(“ NO!! ”) ;
}
const string = (“codedamn”) ;
if (string.endswith(“damn”)) {
  console.log(“ YES!! ”) ;
} else {
  console.log(“ NO!! ”) ;
} 

4) Slicing a string

The process of extracting a substring from the main string can be achieved by the slice() method. We should include the index of the starting and ending letter of the substring you want to extract. The numbering of the string starts with 0. Let us understand with an example.

const string = (“codedamn”) ;
console.log(string.slice(0, 4));

For the above-written code, we will get the output as shown below.

code

There is no need of including a second number if you want to extract all the characters till the end of the string.

5) Changing the case of the string

We use the string methods toLowerCase() and toUpperCase() to change the case of all the characters of the string to lower case and upper case respectively. Let us see the working of this method with the help of an example.

let string = (“ Codedamn is one of the best platforms to learn ”)
console.log(string.toLowerCase());
console.log(string.toUpperCase());

We get the output of the above-written code as shown below.

codedamn is one of the best platforms to learn
CODEDAMN IS ONE OF THE BEST PLATFORMS TO LEARN

6) Replacing a part of the string

We can use the method replace() to replace a part of a string with another substring. We have to include the existing substring that we want to change and the substring with which we want to replace inside the brackets. Let us understand it with an example.

let string = (“examination”);
let new = string.replace( ‘nation’, ‘ner’ );
console.log(new);

We get the output of the above-written code as shown below

examiner

7) String concat

We use the string method concat() to add strings. The use of concat is similar to that of using +. Let us understand it with an example.

let string1 = "Hi";
let string2 = "Friends";
let string3 = string1.concat(" ", string2);
console.log(string3);

We get the output of the above-written code as shown below.

Hi Friends

8) Removing white spaces

We can remove the white spaces from the corners of the string by using the string method trim(). Let us understand it with an example.

string1 = “(       It's a pleasure to meet you.    )”;
string2 = string1.trim();
console.log(string2);

We get the output as:

(It's a pleasure to meet you.)

9) at()

The string method at() takes the value of an integer and returns the character in the string that is bearing that integer. We can also use square brackets for this purpose. We can include both negative and positive integers inside the bracket of at(). The negative integers are counted from the back of the string. Let us understand it with an example.

string = (“ I am a good boy ”)
let index = 4;
console.log( sentence.at(index));

We will get the output as shown below.

g

Let us discuss some of the other important string methods briefly.

String methods
Description
indexOf() Returns the position of the first occurrence of the letter in the string.
lastIndexOf() Returns the position of the last letter of the string.
search() Searches the string for a value
substr() Works like the slice() method.
valueOf() Returns the value of the string.
repeat() Returns a new string which is the old string repeated the number of times written in the bracket.
prototype Allows us to add methods and properties to an object.
split() Splits a string into an array of substrings.

 

This is all about string methods in JavaScript. If you want to learn more about javascript, do check out the article and course on Codedamn of javascript. If you are interested in learning JavaScript, do check out courses on codedamn with an in-built environment playground to learn and practice code. Join the community of codedamn and do check out other articles related to programming and development on codedamn and subscribe to our newsletter to never miss out on our new programs and updates.

Learn advanced theoretical javascript by clicking here!!

Do check out our blog on Prototype in JavaScript.

Sharing is caring

Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: