;

How to Remove the First and Last Character from a String using Javascript


Tutorialsrack 05/06/2021 Jquery Javascript

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().

How to Remove the First Character From a String in Javascript

How to Remove Last Character From String Using Javascript or JQuery

How to Remove the Last Word from the String Using JavaScript

Example 1:  Remove the First and Last Character from a String Using substring() Method

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. 

Example 1:  Remove the First and Last Character from a String Using substring() Method
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"

Example 2:  Remove the First and Last Character from a String Using slice() Method

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.

Example 2:  Remove the First and Last Character from a String Using slice() Method
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"

Example 3:  Remove the First and Last Character from a String Using substr() and slice() Method

In this example, we used the substr() and slice() method to remove the first and last character of a string. 

Example 3:  Remove the First and Last Character from a String Using substr() and slice() Method
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"

Example 4: Another Approach to Remove the First and Last Character from a String

In this example, we used the substring() method and for loop to remove the first and last character of a string. 

Example 4: Another Approach to Remove the First and Last Character from 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!


Related Posts



Comments

Recent Posts
Tags