How to delete "px" from 245px

JavascriptStringVariablesErase

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

To convert 245px in 245 just run:

parseInt('245px', 10);

It retains only leading numbers and discards all the rest.

Solution 2 - Javascript

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

Solution 3 - Javascript

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.

Solution 4 - Javascript

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

s.substr(0, s.length-2);

Solution 5 - Javascript

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.

Solution 6 - Javascript

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

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

Solution 7 - Javascript

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

string.substr(0, string.length - 2)

Solution 8 - Javascript

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

since it's not surrounding the input.

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

Solution 9 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomkayView Question on Stackoverflow
Solution 1 - JavascriptDonView Answer on Stackoverflow
Solution 2 - JavascriptCaspar KleijneView Answer on Stackoverflow
Solution 3 - JavascriptForeeverView Answer on Stackoverflow
Solution 4 - JavascriptJochemView Answer on Stackoverflow
Solution 5 - JavascriptKyle ShayView Answer on Stackoverflow
Solution 6 - JavascriptShoaib AhmadView Answer on Stackoverflow
Solution 7 - JavascriptbrafalesView Answer on Stackoverflow
Solution 8 - JavascriptbArmageddonView Answer on Stackoverflow
Solution 9 - JavascriptElijah MockView Answer on Stackoverflow