;

How to Remove Last Character From String Using Javascript or JQuery


Tutorialsrack 03/06/2021 Jquery Javascript

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

How to Remove the First Character From a String in Javascript

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

How to Remove the Last Word from the String Using JavaScript

Example 1: Remove the Last Character of a String Using substring() Method

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.

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

Example 2: Remove the Last Character of a String Using slice() Method

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.

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

Example 3: Remove the Last Character of a String Using substr() Method

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.

Example 3: Remove the Last Character of a String Using substr() Method
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!


Related Posts



Comments

Recent Posts
Tags