7

4 Ways to Remove the Last Character from a String in JavaScript 🚮

 3 years ago
source link: https://dev.to/gaelgthomas/4-ways-to-remove-the-last-character-from-a-string-in-javascript-50mf
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

A short tutorial on how to get and remove the last character of string in JavaScript.

Remove the last character from String using Slice

The most common way to trim the last character is by using the JavaScript slice method. This method can take up to two indexes as parameters and get the string between these two values.

To keep the whole string and remove the last character, you can set the first parameter to 0 and pass the string length - 1 as the second parameter.

const bookName = 'Atomic Habits' // 13 characters (indexes between 0 and 12)
const newBookName = bookName.slice(0, bookName.length - 1) // Between (0 and 12)

console.log(newBookName)
// Output: "Atomic Habit"
Enter fullscreen modeExit fullscreen mode

As a shortcut, if you pass -1 as the second parameter, it will automatically calculate the string length - 1 for you.

const bookName = 'Atomic Habits' // 13 characters (indexes between 0 and 12)
const newBookName = bookName.slice(0, -1) // Between (0 and 12)

console.log(newBookName)
// Output: "Atomic Habit"
Enter fullscreen modeExit fullscreen mode

If you want to remove two characters at the end, you can do as follow and so on!

const bookName = 'Atomic Habits' // 13 characters (indexes between 0 and 12)
const newBookName = bookName.slice(0, -2) // Between (0 and 11)

console.log(newBookName)
// Output: "Atomic Habi"
Enter fullscreen modeExit fullscreen mode

If you want to have more information, you can read the slice documentation.

Remove the last character from String using Substring or SubStr

Substring method

Another way to delete the last character of a string is by using the substring method. This method will extract characters from a string. It takes as a first parameter the index where you want to start, and as a second, the index where you want to stop.

The idea here is to start from 0 and keep all the letters except the last one. To do that, we will use the length of the string - 1 (index of the last position).

const bookName = 'Atomic Habits'
const newBookName = bookName.substring(0, bookName.length - 1)

console.log(newBookName)
// Output: "Atomic Habit"
Enter fullscreen modeExit fullscreen mode

If you want to have more information, you can read the substring documentation.

Substr method

You can also use the substr method. This method will extract characters from a string. It takes as a first parameter the starting index, and as a second, the number of characters you want to extract.

Here, we want to start from zero and extract all the characters except the last one. We can still use the string length - 1 as a value (from index 0 extracts 10 characters).

const bookName = 'Atomic Habits'
const newBookName = bookName.substr(0, bookName.length - 1)

console.log(newBookName)
// Output: "Atomic Habit"
Enter fullscreen modeExit fullscreen mode

Note: The only difference between subtring and substr is the extraction process. substring uses two indexes while substr uses one index and the number of characters to extract.

If you want to have more information, you can read the substr documentation.

Remove the last character from String using Pop

If you want to trim the last character of your string, you can also make an array conversion using split. Thanks to that, we will use the built-in array functions to remove the last element. Then, we will convert again to a string.

const bookName = 'Atomic Habits'

// We create an array: ['A', 't', 'o', 'm', 'i', 'c', ' ', 'H', 'a', 'b', 'i', 't', 's']
const bookNameArray = bookName.split(''
// We delete the last element (letter 's')
bookNameArray.pop()

// We convert back the array to a string
const newBookName = bookNameArray.join('')

console.log(newBookName)
// Output: "Atomic Habit"
Enter fullscreen modeExit fullscreen mode

What's next?

It's over! 🎉 Now you know how to remove the last character from a string in JavaScript, discover my other article about how to remove the first character from a string in JavaScript.


➡️ I'm starting to tweet more consistently. If you want to get more tips and resources about web programming -> Find me on my Twitter 🐦


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK