gpt4 book ai didi

Javascript在文本上剪切相同的第二个字符

转载 作者:行者123 更新时间:2023-12-02 06:30:17 26 4
gpt4 key购买 nike

例如我有文本:

var x="default_1305, default_1695, default_1805";

我想在第二个逗号之前剪切以获得此文本:“default_1305, default_1695”。

我该怎么做?

最佳答案

var x="default_1305, default_1695, default_1805";

字符串可以被分割,如下所示:

var res = x.split(",", 2);

在第二个参数中注意 2。

如果需要作为字符串,则

var res_string = res.join(",");


编辑:

.split() on MDN

语法

str.split([separator[, limit]]) Parameters

分隔符

Optional. Specifies the character(s) to use for separating the string. The separator is treated as a string or a regular expression. If separator is omitted, the array returned contains one element consisting of the entire string. If separator is an empty string, str is converted to an array of characters.

限制

Optional. Integer specifying a limit on the number of splits to be found. The split() method still splits on every match of separator, until the number of split items match the limit or the string falls short of separator.

关于Javascript在文本上剪切相同的第二个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40043040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com