How to remove first and last character from string in js

Introduction

In this article, you will study how to remove first and last character from string in js (javascript). Strings are the fundamental part of javascript. Character enclosed in a single quote (‘ ‘) or double quote (” “) is known as a string. The string can consist of numbers, whitespace characters, symbols & letters.

A code example for string is given below:

const str = "Hello, World!";

In the above code the string “Hello, World!” is assigned to a const variable str.

Methods

There are multiple methods used to remove first and last character from string in js, The two most common methods used are

  • Slice method
  • Substring method

1) Slice Method

In the slice method, we can easily remove first and last character from string in js (javascript) both at a time and also a single character at a time. We can remove the specific characters by entering the indexes inside the slice method. A code example to remove first and last character from string in js (javascript) by slice method is given below:

const str = "Hello, World!";
function removeFirstAndLastChar(str) {
  return str.slice(1, -1);
}

// Example usage
const originalString = "Hello, World!";
const modifiedString = removeFirstAndLastChar(originalString);
console.log(modifiedString); // Output: "ello, World"
Code Explanation

In the first step, we have defined a string. In the second step, a function name removeFirstAndLastChar & in the parameters we have passed str in it. This function returns the modified string (removed first & last char from str).In the str.slice(1, -1) the 1 is used to remove the first character of the string and -1 is used to remove the last character of the string.

2) Substring method

The substring method is also like the slice method and is commonly used in javascript programming. By using this method we can remove first and last character from string in javascript as shown in the code example below:

const str = "Hello, World!";
function removeFirstAndLastChar(str) {
  return str.substring(1, str.length - 1);
}

// Example usage
const originalString = "Hello, World!";
const modifiedString = removeFirstAndLastChar(originalString);
console.log(modifiedString); // Output: "ello, World"
Code Explanation

In the first step, we have defined a string (str). After that, a function is defined with the name removeFirstAndLastChar & str is passed as a parameter to the function. This function is returning the modified string in which the first and last character is removed. In the str.substring(1, str.length – 1) 1 is used to remove the first character from the string, and str.length -1 is used to remove the last character from the string(str).

Conclusion

The key difference between both is the slice method allows using of both forward and backward indexes (1, -1), where as the substring method checks if the first index is greater than the last index it automatically swaps indexes to ensure they are in the correct order.

Similar Posts