[英]How to get substr of the string's all characters except for the last two digits? (ex: 031p2 >> get 031)
id = '01d0';
document.write('<br/>'+id.substr(0,-2));
How can I get the 01
without the last two chars? 如何在没有最后两个字符的情况下获得
01
? The trick is that if i do it like (0,2)
it does get the first 2 numbers but this id will also be a three digit number... so I just need to get rid of the last two digits. 诀窍是,如果我这样做
(0,2)
它确实获得前2个数字,但这个ID也将是一个三位数...所以我只需要摆脱最后两位数。 This works with substr
in PHP, how come (0,-2)
doesn't work with javascript? 这适用于PHP中的
substr
,怎么来(0,-2)
不能用于javascript? More importantly how can i make this work? 更重要的是我如何才能完成这项工作?
您正在寻找slice()
(另见MDC )
id.slice(0, -2)
尝试id.substring(0, id.length - 2);
var str = "031p2";
str.substring(0, str.length-2);
See : http://jsfiddle.net/GcxFF/ 请参阅: http : //jsfiddle.net/GcxFF/
Something like: 就像是:
id.substr(0, id.length - 2)
The first parameter of substr is the starting index. substr的第一个参数是起始索引。 The second parameter is how many characters to take.
第二个参数是要采用的字符数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.