Categories
javascript slice trim

How do I chop/slice/trim off last character in string using Javascript?

2385

I have a string, 12345.00, and I would like it to return 12345.0.

I have looked at trim, but it looks like it is only trimming whitespace and slice which I don’t see how this would work. Any suggestions?

2

  • 41

    Do you care about rounding? 12345.46 = 12345.5 or 12345.4?

    – RSolberg

    Jun 4, 2009 at 20:40

  • 10

    Do you know what the suffix is or do you want to split and remove the last word based on your underscores?

    – Cᴏʀʏ

    Aug 30, 2010 at 3:07

3750

You can use the substring function:

let str = "12345.00";
str = str.substring(0, str.length - 1);
console.log(str);

This is the accepted answer, but as per the conversations below, the slice syntax is much clearer:

let str = "12345.00";
str = str.slice(0, -1); 
console.log(str);

10

  • 34

    @Kheu – the slice notation is much cleaner to me. I was previously using the substring version. Thanks!

    – Matt Ball

    Apr 13, 2010 at 14:09

  • 38

    forgive me if I’m wrong but don’t you need to assign the value of str.substring to str again? Like str = str.substring(0, str.length -1);

    Jul 15, 2011 at 16:10

  • 13

    The slice & substring methods are all most the same; except the that the slice() accepts a negative index, relative to the end of the string, but not the substring, it throws out-of-bound error

    Apr 9, 2013 at 9:45

  • 28

    In case anybody is wondering, substring is 11% faster than slice. jsperf.com/js-slice-vs-substring-test

    – BenR

    Apr 16, 2014 at 15:53


  • 29

    substring is insane micro-optimizations which I don’t think you should do. Maximize for readability first. Then optimize low hanging fruit if needed.

    – Alfred

    Jul 9, 2014 at 11:10

1476

You can use slice! You just have to make sure you know how to use it. Positive #s are relative to the beginning, negative numbers are relative to the end.

js>"12345.00".slice(0,-1)
12345.0

7

  • 81

    Compared to the accepted solution, this is way more elegant and can be used even with dynamically created strings

    Jul 3, 2012 at 20:41

  • 1

    I like this way because it jives with php thinking for substr function, easier to remember and write on the fly.

    Mar 15, 2013 at 6:24

  • 2

    @SameerAlibhai I agree with you, but couldn’t the substring method be used on dynamic strings too? By using the str.length to dynamically get the length?

    Sep 2, 2014 at 22:41

  • Should be added that the first index is inclusive and the second exclusive.

    – Miscreant

    Jul 7, 2015 at 16:27


  • 2

    This works for removing the last character, but beware if you want to remove a variable number of characters; .slice(0, -0) will return an empty string!

    Apr 23, 2019 at 1:18

275

You can use the substring method of JavaScript string objects:

s = s.substring(0, s.length - 4)

It unconditionally removes the last four characters from string s.

However, if you want to conditionally remove the last four characters, only if they are exactly _bar:

var re = /_bar$/;
s.replace(re, "");

4

  • 108

    slice is better here. s.slice(0, -4)

    – Tim Down

    Aug 30, 2010 at 22:30

  • 14

    Alternatively: s.slice(0, -“_bar”.length) (useful if one doesn’t want to hardcode the number of characters)

    – Mahn

    Aug 11, 2012 at 0:05

  • 4

    I like this one because he also gives help for replacing a specified ending.

    – Mike Graf

    Jul 22, 2013 at 17:43

  • 1

    The second example will only remove the first instance of “_bar” if there are multiple instances in the string

    – O Genthe

    Jun 9, 2021 at 11:22