String methods in Javascript

Strings in Javascript consist of many methods and these methods help you to work with strings.

String length method

The string length property fetches us the length of the string.

const str = "Hello World";
console.log(str.length);

String indexOf() method

We can find out the index of a particular character in a given string using indexOf() method.

const str = "Hello World";
console.log(str.indexOf('e')); //This will console the index of the character 'e'

Let us take the above example again but this time let us find the index of a particular character in the string that occurs more than once. In this case, the indexOf() property will return the index of the first occurrence of the character in the string.

const str ="Hello World";
console.log(str.indexOf('l')); //This will return the index of the first occurence of the character 'l'.

String lastIndexOf() method

The lastIndexOf() property returns the last occurrence of a particular character in a given string.

const str ="Hello World";
console.log(str.lastIndexOf('l'));// This will return the index of the last occurence of the character 'l'.

string slice() method

The slice() method extracts a part of the string and returns the extracted part as a new string. The slice property can have a single argument and can also have two arguments. The slice property takes a number/numbers as arguments.

The syntax of a slice method containing one argument is string.slice(start). Here start refers to the starting index i.e the position from where the extraction should begin.

const str="Hello World";
console.log(str.slice(1)); // This will return ello World

The syntax of the slice method containing two arguments is string.slice(start,end). Here start is the starting index and end is the index upto which the string is extracted. Keep in mind the extracted string will not include the character in the end index but include the characters upto the end index. Also notice that space occupies an index too.

const str ="Hello World";
console.log(str.slice(1,7)); //This will return ello W

Now let's use the above methods together and put them to practice. Let's try to extract the string 'World' from the string 'Hello World' using the above methods.

const str = "Hello World";
console.log(str.slice(str.indexOf('W'),str.length));

String toLowerCase() method

The toLowerCase() method converts all the letters in the string to lowercase letters. One should keep in mind that this method does not change the original string.

const str ="Hello World";
console.log(str.toLowerCase());

Similarly to convert a string to upper case you can use the toUpperCase() method.

String replace() method

The replace method replaces a specific character with the character of your choice.

const str = "Hello World";
console.log(str.replace('e','r'));

In the above code, we have replaced the character e with the character r.

We can also use the replace() method to replace a particular word in a given string. In the below code snippet the word good is replaced with great.

const greeting = "Have a good day!";
console.log(greeting.replace('good','great'));

There are some string methods that return a boolean value namely, includes(), startsWith() and endsWith(). Let's check them out.

String includes() method

The includes() method checks if the input string contains a specific string or a character. It returns true if contains the string and it returns false if it does not contain the string. Let's look at an example.

const str = "Good morning";
if(str.includes("Good"))
 {
    console.log("It contains the word 'Good'");
  }
else
 {
    console.log("It does not contain the word 'Good' ");
 }

In the above code, the string "Good morning" contains the string "Good". Therefore the if condition will result in true.

String startsWith() method & String endsWith() method

The startsWith() method and the endsWith() method will return true if the given input string begins and ends with a specific string. Let's take a look at an example.

const greet = "Good morning. Have a great day";
if(greet.startsWith('Good') && greet.endsWith('day'))
 {
    console.log("This sentence is perfect");
 }
 else
 {
  console.log("This sentence is not perfect");
 }

The above code will return true because the given sentence does begin with "Good" and end with "day". Therefore the if block will be executed.