In this article, you’ll learn how to remove the first and last character from a string using javascript. There are various ways to remove the first and last character from the javascript. This post explains the four possible ways to remove the first and 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 first and 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.
let input = "Tutorialsrack.com"
// Function to Remove Character
function removeFirstLastCharacter(str){
return str.substring(1,str.length-1);
}
let output = removeFirstLastCharacter(input);
console.log(`Output is ${output}`);
// "Output is utorialsrack.co"
In this example, we used the slice()
method to remove the first and last character of a string. The slice()
function 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 removeFirstLastCharacter(str){
return str.slice(1,str.length-1);
}
let output = removeFirstLastCharacter(input);
console.log(`Output is ${output}`);
// "Output is utorialsrack.co"
In this example, we used the substr()
and slice()
method to remove the first and last character of a string.
let input = "Tutorialsrack.com"
// Function to Remove Character
function removeFirstLastCharacter(str){
return str.substr(1).slice(0,-1);
}
let output = removeFirstLastCharacter(input);
console.log(`Output is ${output}`);
// "Output is utorialsrack.co"
In this example, we used the substring()
method and for
loop to remove the first and last character of a string.
let input = "Tutorialsrack.com";
// JavaScript program to remove the first
// and last character of each word in a string.
function removeFirstLastCharacter(str) {
// add a space to the end of the string
str += " ";
var res = "",
w = "";
// traverse the string and extract words
for (var i = 0; i < str.length; i++) {
if (str[i] === " ") {
// excluding the first and
// last character
res += w.substring(1, w.length - 1) + " ";
// clear the word
w = "";
} else {
// else add the character to word
w += str[i];
}
}
return res.trim();
}
let output = removeFirstLastCharacter(input);
console.log(`Output is ${output}`);
// "Output is utorialsrack.co"
I hope this article will help you to understand how to remove the first and 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