Javascript 如何从 245px 中删除“px”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4860244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:27:14  来源:igfitidea点击:

How to delete "px" from 245px

javascriptstringvariableserase

提问by Tomkay

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

删除字符串最后两个字符的简单方法是什么?

回答by Don

To convert 245pxin 245 just run:

245px在 245 中转换,只需运行:

parseInt('245px', 10);

It retains only leading numbers and discards all the rest.

它只保留前导数字并丢弃所有其余数字。

回答by Caspar Kleijne

use

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

where 10 is the radixdefining parseIntis parsing to a decimalvalue

其中 10 是定义解析为十进制值的基数parseInt

use parseInt but don't use parseInt without a radix

使用 parseInt 但不要使用没有基数的parseInt

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

parseInt() 函数解析一个字符串并返回一个整数。

The signature is parseInt(string, radix)

签名是 parseInt(string, radix)

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

第二个参数强制 parseInt 使用以十为基数的编号系统。

  • 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
  • ParseInt() 的默认输入类型是十进制(基数为 10)。
  • 如果数字以“0”开头,则假定为八进制(基数为 8)。
  • 如果以“0x”开头,则假定为十六进制

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

为什么?如果 $(this).attr('num') 是 "08" 没有基数的 parsInt 会变成 0

回答by Foreever

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

转换末尾没有“px”的像素值。使用 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.

注意:如果使用 parseInt,如果值为整数,则该值将是正确的。如果该值是一个小数,如 245.50px,则该值将四舍五入为 245。

回答by Jochem

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

这完全符合您的要求:删除字符串的最后两个字符:

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

回答by Kyle Shay

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 ").

令人惊讶的是, substring 方法s.substr(0, s.length-2);实际上在删除px( 是的,它看起来并不那么干净,如果有空格,它将保留 -- "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 substrtest enough that the parseIntmethod actually becomes superior.

如果您想考虑空格(您可能应该这样做),那么使用该.trim()函数实际上会减慢substr测试速度,从而使该parseInt方法实际上变得更好。

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.

使用的另一个好处parseInt(s, 10)是您还可以获得类型转换,并且可以立即开始将其应用于数学函数。

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

所以最后,这真的取决于你打算对结果做什么。

  • If it is display only, then using the substrmethod withouta 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 substrmethod 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 parseIntroute.
  • 如果它只是显示,那么使用没有修剪的substr方法可能是你最好的选择。
  • 如果您只是想看看没有 px 的值是否与另一个值相同s.substr(0, s.length-2) == 0,那么substr最好使用该方法,因为"250 " == 250(即使有空格)将导致true
  • 如果您想考虑空间的可能性,将其添加到另一个值,或用它计算某些东西,那么您可能需要考虑使用该parseInt路由。

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

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.

jspref 上的测试占了一个空间。我也尝试了一个s.split('px')[0]s.replace(/ *px/g, '')函数,发现两者都变慢了。

Feel free to add additional test cases.

随意添加其他测试用例。

回答by Shoaib Ahmad

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

尽管 parseInt() 是一个不错的选择,但仍有许多其他解决方案

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

回答by brafales

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

检查http://www.w3schools.com/jsref/jsref_substr.asp在你的情况下会像

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

回答by bArmageddon

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

我更喜欢: "245px".replace(/px/,'')*1

since it's not surrounding the input.

因为它不在输入周围。

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

此外,*1用于将其转换为 int。