4 Ways To Delete First and Last Character From String Using JavaScript.

0

In this blog post you’ll learn how to remove the first and last characters of the string using JavaScript. We’ll see different approaches to extract the first and last character from a string.


delete first and last character from string


JavaScript method used:

replace()
CharAt()
slice()
substring()
substr()


Approach 1: Using a For Loop

We’ll use a for loop to iterate through the characters in the string. With the loop, we add the whole  string in a new string to store all the characters except the first and last ones. This is achieved by skipping the first character and the last character during the iteration.


JavaScript


    let string = "Hello good morning";
    let remove_last_character = "";
    let remove_first_character = "";
    for (let i = 0; i < string.length; i++) {
        const element = string[i];
        if(i !== string.length- 1){
            remove_last_character += element;
        }
        if(i !== 0){
            remove_first_character += element;
        }
    }
    console.log(remove_last_character);
    // Output:
    // Hello good mornin
    console.log(remove_first_character);
    // Output
    // ello good morning

  


Approach 2: With slice() Method.

The slice() method extracts a portion of a string and returns it as a new string. It takes two arguments: the starting index and the ending index (exclusive). The starting index specifies the character at which the extraction begins, while the ending index specifies the character up to, but not including, where the extraction ends. Negative values for the starting or ending index can be used to select characters from the end of the string.


To remove the first character, provide a starting position of 1 (the second character) and an ending position equal to the string's length. This effectively excludes the first character from the result.


Similarly, to remove the last character, use a starting position of 0 (the first character) and an ending position one less than the string's length. This excludes the last character from the slice.


See below code:


JavaScript


    let string = "Hello Good Morning";
    let remove_first_character = string.slice(1, string.length);
    let remove_last_character = string.slice(0, string.length - 1);
    // Output:
    console.log(remove_first_character);
    // ello Good Morning
    console.log(remove_last_character);
    // Hello Good Mornin

  


Approach 3: With substring() Method.

The substring method extracts characters from a string between two specified positions (start and end). By providing the starting index 1, which represents the second character in a zero-based system  and ending index string length, we effectively retrieve the entire string excluding the first character. This approach allows for the removal of the first character using the substring method.


Similarly, to remove the last character from a string, we can specify the starting position as 0 (the first character) and the ending position as the length of the string minus 1 (representing the character before the last). This approach results in a substring that excludes the final character.


See below code.


JavaScript


    let string = "Good morning";
    let remove_first_character = string.substring(1, string.length);
    let remove_last_character = string.substring(0, string.length - 1);
    
    // Output:
    console.log(remove_first_character);
    // ood morning
    console.log(remove_last_character);
    // Good mornin

  


Approach 4: with substr() Method.

The substring method extracts a portion of a string starting from a specified index position and continuing for a defined length.


To delete the first character from a string, use the substring method with a starting index of 1 (the second character) and a length equal to the original string's length.


Similarly, to delete the last character from a string, use the substring method with a starting index of 0 (the first character) and a length equal to the original string's length minus 1.


See below code.


JavaScript


    let string = "Hello good morning";
    let remove_first_character = string.substr(1, string.length);
    let remove_last_character = string.substr(0, string.length-1);

    // Output:
    console.log(remove_first_character);
    // ello good morning
    console.log(remove_last_character);
    // Hello good mornin

  

So that's how you can delete the first and last character from the string using JavaScript. Feel free to ask any questions or suggestions in the comment section.


You may like:

  1. How to create extract first and last character from string using JavaScript.
  2. Remove empty spaces from string using JavaScript.
  3. Remove numeric value from string using JavaScript.
  4. Check string is a number or not using JavaScript.
  5. Count the number of spaces in a string using JavaScript.
  6. Check the presence of special characters in string using JavaScript.
  7. Make input field to accept only numeric values using JavaScript.
  8. Convert or split the string into an array of words using JavaSript.

Tags

Post a Comment

0Comments

Share your thoughts.

Post a Comment (0)
To Top