Friday, January 10, 2025
Google search engine
HomeLanguagesJavascriptHow to Get the Last N Characters of a String in JavaScript

How to Get the Last N Characters of a String in JavaScript

In this article, we will learn how to get the last N characters of a string in JavaScript. We have given a string and we need to get the ā€œnā€ character from the last of the string. There are various methods for finding the ā€œnā€ character from the last of the string.

Below are the following methods through which we get the last N characters of a string in JavaScript:

  • Using substring() Method
  • Using slice() Method
  • Using loop and string concatenation

Approach 1: Using substring() Method in JavaScript

In this approach, we will use the substring() method for removing the last n character of the string. TheĀ string.substring()Ā is an inbuilt function in JavaScript that is used to return the part of the given string from the start index to the end index. Indexing starts from zero (0).Ā By calculating the starting index as ā€œstr.length - n", we get the last n character.

Syntax:Ā 

string.substring( Startindex, Endindex )

Example:

Javascript




function getLastCharacter(str,n) {
Ā Ā Ā Ā let newString = str.substring(str.length - n);
Ā Ā Ā Ā return newString;
Ā Ā 
}
let str = "Geeksforneveropen";
let n=5;
console.log(getLastCharacter(str,n));


Output

neveropen

Approach 2: Using slice() Method in JavaScript

In this approach, we will use the slice method for removing the last n character of the string. TheĀ string.slice()Ā is an inbuilt method in javascript that is used to return a part or slice of the given input string.

Syntax:

string.slice( startingIndex, endingIndex )

Example:

Javascript




function getCharacter(str,n) {
Ā Ā Ā Ā let newString = str.slice(-n);
Ā Ā Ā Ā return newString;
Ā Ā 
}
let str = "Hello Geeks!";
let n=6
console.log(getCharacter(str,n));


Output

Geeks!

Approach 3: Using loop and string concatenation

In this approach, we will use for loop to iterate over the string but in this we iterate from the index str.length-n to the end. So that we can get the last n character of a string.

Example:

Javascript




function getCharacter(str, n) {
Ā Ā Ā Ā let newString = '';
Ā Ā Ā Ā for (let i = str.length - n; i < str.length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā newString += str[i];
Ā Ā Ā Ā }
Ā Ā Ā Ā return newString;
Ā Ā 
}
let str = "Hello Geeks!";
let n = 6
console.log(getCharacter(str, n));


Output

Geeks!
Whether youā€™re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions weā€™ve already empowered, and weā€™re here to do the same for you. Donā€™t miss out ā€“ check it out now!

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?