我有一个字符串,12345.00
并且我希望它返回12345.0
。
我已经看过了trim
,但是看起来它只是在修剪空白,slice
而我看不出它是如何工作的。有什么建议么?
我有一个字符串,12345.00
并且我希望它返回12345.0
。
我已经看过了trim
,但是看起来它只是在修剪空白,slice
而我看不出它是如何工作的。有什么建议么?
Try this:
<script>
var x="foo_foo_foo_bar";
for (var i=0; i<=x.length; i++) {
if (x[i]=="_" && x[i+1]=="b") {
break;
}
else {
document.write(x[i]);
}
}
</script>
You can also try the live working example on http://jsfiddle.net/informativejavascript/F7WTn/87/.
if(str.substring(str.length - 4) == "_bar")
{
str = str.substring(0, str.length - 4);
}
In cases where you want to remove something that is close to the end of a string (in case of variable sized strings) you can combine slice() and substr().
I had a string with markup, dynamically built, with a list of anchor tags separated by comma. The string was something like:
var str = "<a>text 1,</a><a>text 2,</a><a>text 2.3,</a><a>text abc,</a>";
To remove the last comma I did the following:
str = str.slice(0, -5) + str.substr(-4);
The shortest way:
str.slice(0, -1);
If you want to do generic rounding of floats, instead of just trimming the last character:
var float1 = 12345.00,
float2 = 12345.4567,
float3 = 12345.982;
var MoreMath = {
/**
* Rounds a value to the specified number of decimals
* @param float value The value to be rounded
* @param int nrDecimals The number of decimals to round value to
* @return float value rounded to nrDecimals decimals
*/
round: function (value, nrDecimals) {
var x = nrDecimals > 0 ? 10 * parseInt(nrDecimals, 10) : 1;
return Math.round(value * x) / x;
}
}
MoreMath.round(float1, 1) => 12345.0
MoreMath.round(float2, 1) => 12345.5
MoreMath.round(float3, 1) => 12346.0
EDIT: Seems like there exists a built in function for this, as Paolo points out. That solution is obviously much cleaner than mine. Use parseFloat followed by toFixed
const str = "test!";
console.log(str.slice(0, -1));
尝试这个:
const myString = "Hello World!";
console.log(myString.slice(0, -1));
console.log("a string".match(/(.*).$/)[1]);
console.log("a string".match(/(.*).$/));
console.log("a string".match(/(.*).{2}$/)[1]);
使用正则表达式:
let aStr = "12345.00";
aStr = aStr.replace(/.$/, '');
console.log(aStr);
怎么样:
let myString = "12345.00";
console.log(myString.substring(0, myString.length - 1));
正则表达式是您要查找的内容:
let str = "foo_bar";
console.log(str.replace(/_bar$/, ""));
最简单的方法是使用slice
字符串的方法,该方法允许使用负数位置(对应于字符串末尾的偏移量):
const s = "your string";
const withoutLastFourChars = s.slice(0, -4);
如果您需要更通用的方法来删除最后一个下划线(包括下划线)之后的所有内容,可以执行以下操作(只要s
保证至少包含一个下划线):
const s = "your_string";
const withoutLastChunk = s.slice(0, s.lastIndexOf("_"));
console.log(withoutLastChunk);
您可以使用JavaScript字符串对象的substring方法:
s = s.substring(0, s.length - 4)
它无条件删除string中的最后四个字符s
。
但是,如果您要有条件地删除最后四个字符,则前提是它们恰好是 _bar
:
var re = /_bar$/;
s.replace(re, "");
您可以使用slice!您只需要确保您知道如何使用它即可。正号是相对于开始,负号是相对于结束。
js>"12345.00".slice(0,-1)
12345.0
Use substring to get everything to the left of _bar. But first you have to get the instr of _bar in the string:
3 is that start and 7 is the length.