JavaScript Program to Remove Last Character from the String
In this article, we will learn how to remove the last character from the string in JavaScript. The string is used to represent the sequence of characters. Now, we will remove the last character from this string.
Example:
Input : Geeks for geeks Output : Geeks for geek Input : Geeksforgeeks Output : Geeksforgeek
Below are the following approaches through which we can remove the last character from the string:
Table of Content
Approach 1: Using for loop
In this approach, we will use a brute force approach for removing the character from the string. We run the loop through the string and iterate over all the characters except the last character. Now, return the modified string.
Example:
function removeCharacter(str) {
let n = str.length;
let newString = "";
for (let i = 0; i < n - 1; i++) {
newString += str[i];
}
return newString;
}
let str = "Geeksforgeeks";
console.log(removeCharacter(str));
Output
Geeksforgeek
Approach 2: Using the slice() Method
In this approach, we will slice method for removing the last 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:
function removeCharacter(str) {
let newString = str.slice(0, -1);
return newString;
}
let str = "Geeksforgeeks";
console.log(removeCharacter(str));
Output
Geeksforgeek
Approach 3: Using substring() Method
In this approach, we will use substring() method for removing last character of 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 start from zero (0).
Syntax:
string.substring( Startindex, Endindex )
Example:
function removeCharacter(str) {
let newString = str.substring(0, str.length - 1);
return newString;
}
let str = "Geeksforgeeks";
console.log(removeCharacter(str));
Output
Geeksforgeek
Approach 4: Using split() and join() Method
In this approach, we will split the string and then we use pop() method for removing the last character and then we will use join() method for joining the array back.
Example:
function removeCharacter(str) {
let splitString = str.split('')
splitString.pop();
return splitString.join('');
}
let str = "Geeksforgeeks";
console.log(removeCharacter(str));
Output
Geeksforgeek
Approach 5: Using Regular Expression
The regular expression approach uses replace() with a regex pattern targeting the last character (.$) and replacing it with an empty string. This effectively removes the last character from the string, providing a concise solution.
function removeLastCharacter(str) {
return str.replace(/.$/, '');
}
const input1 = 'Geeks for geeks';
const output1 = removeLastCharacter(input1);
console.log(output1);
const input2 = 'Geeksforgeeks';
const output2 = removeLastCharacter(input2);
console.log(output2);
Output
Geeks for geek Geeksforgeek
Approach 6: Using Array.from() Method
In this approach, we will convert the string into an array using the Array.from() method. This method creates a new array from an array-like or iterable object. Once the string is converted to an array, we can use the pop() method to remove the last character and then convert the array back to a string using the join() method.
Example: This method provides a straightforward way to remove the last character by leveraging the flexibility of arrays and their built-in methods
function removeCharacter(str) {
let arr = Array.from(str);
arr.pop();
return arr.join('');
}
let str = "Geeksforgeeks";
console.log(removeCharacter(str));
// Output
// Geeksforgeek
Output
Geeksforgeek