Horje
JavaScript Program to Remove First and Last Characters from a String

This article will show you how to remove the first and last characters from a given string in JavaScript. There are two methods to remove the first and last characters in a string.

1. Using String slice() Method

The string.slice() method returns a part or slice of the given input string.

Syntax:

string.slice(startingIndex, endingIndex)

Example: In this example, we will remove the first and the last character of the string using the slice method in JavaScript.

JavaScript
// JavaScript Program to remoove first 
// and last character of a string
function removeFirstLast(str) {
    return str.slice(1, -1);
}

// Driver code
const str = 'GeeksforGeeks';

console.log(removeFirstLast(str));

Output
eeksforGeek

2. Using String substring() Method

The string.substring() method returns the part of a given string from the start index to the end index. Indexing always start from zero (0).

Syntax:

string.substring(Startindex, Endindex)

Example: In this example, we will remove the first and the last character of the string using the substring() method in JavaScript.

JavaScript
// JavaScript Program to remoove first 
// and last character of a string
function removeFirstLast(str) {
    return str.substring(1, str.length - 1);
}

// Driver code
const str = 'GeeksforGeeks';

console.log(removeFirstLast(str));

Output
eeksforGeek

3. Using Array Methods

To remove the first and last characters from a string using array methods, you can convert the string to an array, remove the first and last elements with shift() and pop(), then join the array back into a string.

Example:

JavaScript
function removeFirstAndLast(str) {
  const arr = str.split('');
  arr.shift();
  arr.pop();
  return arr.join('');
}

const result = removeFirstAndLast("Hello");
console.log(result); // "ell"

Output
ell

4. Using String substr() Method:

The substr() method returns the characters in a string beginning at the specified location through the specified number of characters.

Example: In this example, we will remove the first and last characters of the string using the substr() method in JavaScript.

JavaScript
// JavaScript Program to remove first 
// and last character of a string
function removeFirstLast(str) {
    return str.substr(1, str.length - 2);
}

// Driver code
const str = 'GeeksforGeeks';

console.log(removeFirstLast(str));

5. Using Regular Expression and String Replace Method:

In this method, we can utilize a regular expression along with the string replace() method to remove the first and last characters from a string.

Example: In this example, we will remove the first and last characters of the string using a regular expression with the replace() method in JavaScript.

JavaScript
// Function to remove the first and last characters using regular expression and replace() method
function removeFirstAndLastWithRegex(str) {
    return str.replace(/^.(.*).$/, '$1');
}

// Example usage
const originalStr = "example";
const modifiedStr = removeFirstAndLastWithRegex(originalStr);
console.log(modifiedStr); // Output: xampl

Output
xampl

6. Using shift and pop method:

In this approach we first splits the string into an array of characters and then we use shift() method to remove first element and pop() method to remove to remove last element, and then we joins the remaining elements back into a string.

Example: In this example, we will remove the first and last characters of the string using a shift and pop methods.

JavaScript
function removeFirstAndLastChars(str) {
    if (str.length <= 2) {
        return ''; 
        }
    let arr = str.split('');
    arr.pop(); // Remove the last character
    arr.shift(); // Remove the first character
    return arr.join('');
}

const input = "horje";
const result = removeFirstAndLastChars(input);
console.log(result);

Output
eeksforgeek

7.Using String Destructuring and Slice

In this approach, we use JavaScript’s destructuring assignment to split the string into an array of characters. Then, we use the slice method to remove the first and last characters from the array. Finally, we join the remaining characters back into a string.

Example: This example demonstrates how to remove the first and last characters using string destructuring and slice.

JavaScript
function removeFirstAndLastChar(str) {
    // Convert string to array and remove first and last characters
    let charArray = [...str];
    let newArray = charArray.slice(1, -1);

    // Convert array back to string
    return newArray.join('');
}

// Example usage:
let inputString = "HelloWorld";
console.log(removeFirstAndLastChar(inputString)); // Output: "elloWorl"

Output
elloWorl

8. Using String replace Method without Regular Expressions

In this approach, we can use the replace method to remove the first and last characters by specifying the characters to be replaced as an empty string.

Example: This example demonstrates how to remove the first and last characters using the replace method.

JavaScript
function removeFirstLast(str) {
    if (str.length <= 2) {
        return '';
    }
    return str.replace(str[0], '').replace(str[str.length - 1], '');
}
const str = 'GeeksforGeeks';
console.log(removeFirstLast(str));

Output
eekforGeeks





Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
JavaScript Program to Find Second Largest Element in an Array JavaScript Program to Find Second Largest Element in an Array
JavaScript Program to Find Next Smaller Element JavaScript Program to Find Next Smaller Element
Opposite Words in English: A to Z List, For Class 1 to Class 3 Opposite Words in English: A to Z List, For Class 1 to Class 3
How To Add Strikethrough On Text In ReactJs ? How To Add Strikethrough On Text In ReactJs ?
Functions and Features of a Web Browser Functions and Features of a Web Browser

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12