JavaScript Program to Remove Last Character from the String
Last Updated :
17 Jun, 2024
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:
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:
JavaScript
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));
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:
JavaScript
function removeCharacter(str) {
let newString = str.slice(0, -1);
return newString;
}
let str = "Geeksforgeeks";
console.log(removeCharacter(str));
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:
JavaScript
function removeCharacter(str) {
let newString = str.substring(0, str.length - 1);
return newString;
}
let str = "Geeksforgeeks";
console.log(removeCharacter(str));
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:
JavaScript
function removeCharacter(str) {
let splitString = str.split('')
splitString.pop();
return splitString.join('');
}
let str = "Geeksforgeeks";
console.log(removeCharacter(str));
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.
JavaScript
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);
OutputGeeks 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
JavaScript
function removeCharacter(str) {
let arr = Array.from(str);
arr.pop();
return arr.join('');
}
let str = "Geeksforgeeks";
console.log(removeCharacter(str));
// Output
// Geeksforgeek
Similar Reads
JavaScript- Remove Last Characters from JS String These are the following ways to remove first and last characters from a String:1. Using String slice() MethodThe slice() method returns a part of a string by specifying the start and end indices. To remove the last character, we slice the string from the start (index 0) to the second-to-last charact
2 min read
JavaScript - How to Get the Last N Characters of a String? Here are the different methods to get the last N characters of a string in JavaScript.1. Using slice() MethodThe slice() method is the most commonly used approach for extracting the last N characters, thanks to its simplicity and support for negative indices.JavaScriptconst getChar = (s, n) => s.
2 min read
PHP Program to Remove Last Character from the String Given a String, the task is to remove the last character from a string in PHP. This operation is common in text processing and manipulation. Examples: Input: str = "Hello Geeks!"Output: Hello GeeksInput: str = "Welcome"Output: WelcomThese are the following approaches: Table of Content Using substr()
2 min read
JavaScript - How To Get The Last Caracter of a String? Here are the various approaches to get the last character of a String using JavaScript.1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.JavaScriptconst s = "JavaScript"; co
3 min read
Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t
3 min read
JavaScript - How to Find the First and Last Character of a String? Here are the various methods to find the first and last character of a string in JavaScript.1. Using charAt() MethodThe charAt() method retrieves the character at a specified index.JavaScriptconst s = "JavaScript"; const first = s.charAt(0); const last = s.charAt(s.length - 1); console.log(first); c
2 min read
How to remove the first character of string in PHP? Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation:In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. re
3 min read
JavaScript - Delete Character from JS String In JavaScript, characters can be deleted from the beginning, end, or any specific position in a string. JavaScript provides several methods to perform these operations efficiently.Delete First CharacterTo remove the first character from a string, we can use methods like slice, substring, or regular
2 min read
How to get the last character of a string in PHP ? In this article, we will find the last character of a string in PHP. The last character can be found using the following methods.Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in
2 min read
JavaScript - Delete First Character of a String To delete the first character of a string in JavaScript, you can use several methods. Here are some of the most common onesUsing slice()The slice() method is frequently used to remove the first character by returning a new string from index 1 to the end.JavaScriptlet s1 = "GeeksforGeeks"; let s2 = s
1 min read