JavaScript截断/切片/修剪掉字符串中的最后一个字符

我有一个字符串,12345.00并且我希望它返回12345.0

我已经看过了trim,但是看起来它只是在修剪空白,slice而我看不出它是如何工作的。有什么建议么?

Mandy小卤蛋凯2020/03/09 19:11:33

Use substring to get everything to the left of _bar. But first you have to get the instr of _bar in the string:

str.substring(3, 7);

3 is that start and 7 is the length.

ProGreen2020/03/09 19:11:33

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/.

LGO西里2020/03/09 19:11:33
if(str.substring(str.length - 4) == "_bar")
{
    str = str.substring(0, str.length - 4);
}
前端Eva2020/03/09 19:11:33

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);
Eva千羽2020/03/09 19:11:33

The shortest way:

str.slice(0, -1); 
猿AJim2020/03/09 19:11:33

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

达蒙神奇2020/03/09 19:11:33

const str = "test!";
console.log(str.slice(0, -1));

小卤蛋Davaid2020/03/09 19:11:33

尝试这个:

const myString = "Hello World!";
console.log(myString.slice(0, -1));

YOC388805452020/03/09 19:11:33
  1. (。*),多次捕获任何字符

console.log("a string".match(/(.*).$/)[1]);

  1. 。,匹配最后一个字符,在这种情况下

console.log("a string".match(/(.*).$/));

  1. $,匹配字符串的结尾

console.log("a string".match(/(.*).{2}$/)[1]);

十三西门2020/03/09 19:11:33

使用正则表达式:

let aStr = "12345.00";
aStr = aStr.replace(/.$/, '');
console.log(aStr);

Itachi泡芙Pro2020/03/09 19:11:33

怎么样:

let myString = "12345.00";
console.log(myString.substring(0, myString.length - 1));

小宇宙2020/03/09 19:11:33

正则表达式是您要查找的内容:

let str = "foo_bar";
console.log(str.replace(/_bar$/, ""));

小卤蛋小卤蛋小哥2020/03/09 19:11:33

最简单的方法是使用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);

小宇宙猴子2020/03/09 19:11:33

您可以使用JavaScript字符串对象substring方法:

s = s.substring(0, s.length - 4)

它无条件删除string中的最后四个字符s

但是,如果您要有条件地删除最后四个字符,则前提是它们恰好是 _bar

var re = /_bar$/;
s.replace(re, "");
Jim猪猪2020/03/09 19:11:33

您可以使用slice您只需要确保您知道如何使用它即可。正号是相对于开始,负号是相对于结束。

js>"12345.00".slice(0,-1)
12345.0
2020/03/09 19:11:33

您可以使用substring函数:

let str = "12345.00";
str = str.substring(0, str.length - 1);
console.log(str);

这是公认的答案,但是根据下面的对话,切片语法更加清晰:

let str = "12345.00";
str = str.slice(0, -1); 
console.log(str);