我有一个带有一定数量逗号的字符串。我想找到第5个逗号的索引,然后将其拼接。我该怎么做?
像这样的字符串: "This, is, my, javascript, string, to, present"
变成这个: "This, is, my, javascript, string to, present"
var str = "This, is, my, javascript, string, to, present";
var i = 0, c = 5; // for flexibility c could be any number (3 for the third, ...)
while((i = str.indexOf(',', i + 1)) !== -1 && --c) // look for the fifth comma if any
;
if(i != -1) // if there is a fifth comma
str = str.substr(0, i) + str.substr(i + 1); // then remove it
console.log(str);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句