91

Whats a simple way to delete the last two characters of a string?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tomkay
  • 5,120
  • 21
  • 60
  • 92

9 Answers9

209

To convert 245px in 245 just run:

parseInt('245px', 10);

It retains only leading numbers and discards all the rest.

jAndy
  • 231,737
  • 57
  • 305
  • 359
Don
  • 16,928
  • 12
  • 63
  • 101
  • 7
    This is the most malleable way, but you should always specify the radix `parseInt('245px', 10)` – Justin Johnson Feb 01 '11 at 08:14
  • You're right: the (strange) default is octal when the string starts with '0'... How many times I got an error for `parseInt('09')`! – Don Feb 01 '11 at 08:15
  • 4
    what do you do when value is like 35.5px..using parseInt would result in 35 only – Muhammad Umer May 11 '14 at 17:08
  • 6
    @MuhammadUmer: you can use 'parseFloat' (http://www.w3schools.com/jsref/jsref_parsefloat.asp) – Don May 11 '14 at 20:23
42

use

var size = parseInt('245px', 10);

where 10 is the radix defining parseInt is parsing to a decimal value

use parseInt but don't use parseInt without a radix

The parseInt() function parses a string and returns an integer.

The signature is parseInt(string, radix)

The second argument forces parseInt to use a base ten numbering system.

  • The default input type for ParseInt() is decimal (base 10).
  • If the number begins in "0", it is assumed to be octal (base 8).
  • If it begins in "0x", it is assumed to be hexadecimal

why? if $(this).attr('num') would be "08" parsInt without a radix would become 0

Community
  • 1
  • 1
Caspar Kleijne
  • 21,552
  • 13
  • 72
  • 102
25

To convert a pixel value without the "px" at the end. use parseFloat.

parseFloat('245px'); // returns 245      

Note: If you use parseInt, the value will be correct if the value is an integer. If the value is a decimal one like 245.50px, then the value will be rounded to 245.

Wax
  • 605
  • 1
  • 5
  • 15
Foreever
  • 7,099
  • 8
  • 53
  • 55
  • As my edit to make a precision was rejected, I'm gonna put this in comment, `parseFloat()` does not take radix like `parseInt()` (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat & https://stackoverflow.com/questions/8555649/second-argument-to-parsefloat-in-javascript#answer-8555676) – Wax Mar 18 '20 at 17:47
17

This does exactly what you ask: remove last two chars of a string:

s.substr(0, s.length-2);
Jochem
  • 2,995
  • 16
  • 18
6

Surprisingly, the substring method s.substr(0, s.length-2); is actually quite a bit faster for removing the px (yes it isn't as clean looking, and if there is a space it will remain -- "250px" -> "250" vs "250 px" -> "250 ").

If you want to account for spaces (which you probably should) then using the .trim() function will actually slow down the substr test enough that the parseInt method actually becomes superior.

An added benefit of using parseInt(s, 10) is that you also get a type conversion and can immediately start to apply it to mathematical functions.

So in the end, it really depends on what you plan on doing with the result.

  • If it is display only, then using the substr method without a trim would probably be your best bet.
  • If you're just trying to see if the value without px is the same as another value s.substr(0, s.length-2) == 0, then using the substr method would be best, as "250 " == 250 (even with the space) will result as true
  • If you want to account for the possibility of a space, add it to another value, or to compute something with it, then you may want to consider going with the parseInt route.

http://jsperf.com/remove-px-from-coord

The tests on jspref account for a space. I also tried a s.split('px')[0] and s.replace(/ *px/g, '') function, both found to be slower.

Feel free to add additional test cases.

Kyle Shay
  • 688
  • 6
  • 9
4

Although parseInt() is a good option but still it is good to have many other solutions

var pixels = '245px';
Number(pixels.replace('px', ''));
1

substr() is now a legacy feature; use substring() instead: (syntax is the same in this case)

str.substring(0, str.length-2);

Or, use slice():

str.slice(0, -2);

slice() looks much cleaner, IMO. Negative values count back from the end.

Elijah Mock
  • 587
  • 8
  • 21
0

Check http://www.w3schools.com/jsref/jsref_substr.asp In your case would be something like

string.substr(0, string.length - 2)
brafales
  • 570
  • 3
  • 6
  • 22
0

I prefer: "245px".replace(/px/,'')*1

since it's not surrounding the input.

Also, the *1 is for casting it to int.

bArmageddon
  • 8,088
  • 6
  • 22
  • 40