In this article, you will learn how to remove the last character from the string using javascript or Jquery. There are various ways to remove the last character from the string using javascript or jquery. This post explains the three possible ways to remove the last character from the string using javascript or Jquery. JavaScript Provides a wide range of built-in string methods. There are three straightforward methods: substring()
, slice()
, or substr()
In this example, we used the substring()
method to remove the last character of a string. The substring()
method returns the part of the string between the specified indexes. This function takes two parameters, the starting point of the substring, and an ending point of the substring. 0 as the starting point, and the length of the original string minus 1 as an ending point. This will return the original string after removing the last character of the string.
let input = "Tutorialsrack.com"
// Function to Remove Character
function removeLastCharacter(str){
return str.substring(0, str.length - 1);
}
let output = removeLastCharacter(input);
console.log(`Output is ${output}`);
// "Output is Tutorialsrack.co"
In this example, we used the slice()
method to remove the last character of a string. The slice()
method works in a similar way. This method extracts the text from a string between the specified indexes and returns a part of the substring.
The first parameter is the index where to begin the extraction. However, the second parameter is optional, where to end the extraction.
let input = "Tutorialsrack.com"
// Function to Remove Character
function removeLastCharacter(str){
return str.slice(0, -1);
}
let output = removeLastCharacter(input);
console.log(`Output is ${output}`);
// "Output is Tutorialsrack.co"
In this example, we used the substr()
method to remove the last character of a string. The substr()
method returns a part of the string, starting at the specified index and extracting the specified number of characters.
let input = "Tutorialsrack.com"
// Function to Remove Character
function removeLastCharacter(str){
return str.substr(0, str.length - 1);
}
let output = removeLastCharacter(input);
console.log(`Output is ${output}`);
// "Output is Tutorialsrack.co"
I hope this article will help you to understand how to remove the last character from a string in Javascript or Jquery.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments