我有一个像下面的字符串,我想用数字和“+”拆分,我尝试使用下面的代码,
输入字符串: 20001 + 20002 + 20003 + 20005 + 20019 + 20035 + 20009 + 20011 + 20015 + 20006 + 20020 + 20047 + 20048 + 20050 + 20049 + 204044 + 22407 + 20052 + 20057 + 20058 + 20059 + 20063 + 20065 + 20067 + 20068 + 20070 + 20072 + 20073 + 20075 + 20076 + 20078 + 20081 + 20084 + 20085 + 20086 + 20140 + 21954 + 206171 + 206170 + 206175 + 20093 + 206168 + 206177 + 206172 + 20098 + 206167 + 20107 + 20053 + 20054 + 20056 + 20108 + 20109 + 20110 + 20112 + 20115 + 20117 + 20119 + 20124 + 20126 + 20131 + 20132 + 20136 + 20141 + 20344 + 20345 + 20346 + 20348 + 20349 + 20355 + 20356.A
代码:
First found the len of the string,
var str1 = 20001+20002+20003+20005+20019+20035+20009+20011+20015+20006+20020+20047+20048+20050+20049+204044+22407+20052+20057+20058+20059+20063+20065+20067+20068+20070+20072+20073+20075+20076+20078+20081+20084+20085+20086+20140+21954+206171+206170+206175+20093+206168+206177+206172+20098+206167+20107+20053+20054+20056+20108+20109+20110+20112+20115+20117+20119+20124+20126+20131+20132+20136+20141+20344+20345+20346+20348+20349+20355+20356.A
str2 = str1.length;
if (str2 > '400') {
var str3 = str1.split("+", 100);
}else{
var str3 = str1
}
期望的输出:
str3[0] = 20001+20002+20003+20005+20019+20035+20009+20011+20015+20006+20020+20047+20048+20050+20049+204044+22407
str3[1] = 20052+20057+20058+20059+20063+20065+20067+20068+20070+20072+20073+20075+20076+20078+20081+20084+20085
str3[2] = 20086+20140+21954+206171+206170+206175+20093+206168+206177+206172+20098+206167+20107+20053+20054
str3[3] = 20056+20108+20109+20110+20112+20115+20117+20119+20124+20126+20131+20132+20136+20141+20344+20345
str3[4] = 20346+20348+20349+20355+20356.A
默认长度在这里是100,它应该根据字符串减少而不是增加(帮助需要完成此操作)
请在一些指导下帮助我
答案 0 :(得分:1)
您可以使用String#indexOf
作为fromIndex
的正确起始值来搜索下一个+
并为部分切换字符串。
var string = '20001+20002+20003+20005+20019+20035+20009+20011+20015+20006+20020+20047+20048+20050+20049+204044+22407+20052+20057+20058+20059+20063+20065+20067+20068+20070+20072+20073+20075+20076+20078+20081+20084+20085+20086+20140+21954+206171+206170+206175+20093+206168+206177+206172+20098+206167+20107+20053+20054+20056+20108+20109+20110+20112+20115+20117+20119+20124+20126+20131+20132+20136+20141+20344+20345+20346+20348+20349+20355+20356.A',
length = 100,
start = 0,
pos,
result = [];
while ((pos = string.indexOf('+', start + length)) !== -1) {
result.push(string.slice(start, pos));
start = pos + 1;
}
result.push(string.slice(start));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
与Nina Scholz的回答差不多,但有点不同。从0开始然后查找" +"在接下来的100个字符之后,然后将其复制到结果数组中。在" +"之后从角色再次开始直到字符串用尽。
var s = '20001+20002+20003+20005+20019+20035+20009+20011+20015+20006+20020+20047+20048+20050+20049+204044+22407+20052+20057+20058+20059+20063+20065+20067+20068+20070+20072+20073+20075+20076+20078+20081+20084+20085+20086+20140+21954+206171+206170+206175+20093+206168+206177+206172+20098+206167+20107+20053+20054+20056+20108+20109+20110+20112+20115+20117+20119+20124+20126+20131+20132+20136+20141+20344+20345+20346+20348+20349+20355+20356.A';
var start = 0,
min = 100,
pos = 0,
result = [];
while (pos != -1) {
pos = s.indexOf('+', start + min);
result.push(s.substring(start, pos == -1? s.length : pos));
start = pos+1;
}
console.log(result);

答案 2 :(得分:0)
这是您在评论中寻找解释的原因
var s = "20001+20002+20003+20005+20019+20035+20009+20011+20015+20006+20020+20047+20048+20050+20049+204044+22407+20052+20057+20058+20059+20063+20065+20067+20068+20070+20072+20073+20075+20076+20078+20081+20084+20085+20086+20140+21954+206171+206170+206175+20093+206168+206177+206172+20098+206167+20107+20053+20054+20056+20108+20109+20110+20112+20115+20117+20119+20124+20126+20131+20132+20136+20141+20344+20345+20346+20348+20349+20355+20356.A"
var d = [];
var slug = 100;//threshold value for separatuion
var rounds = Math.ceil(s.length/slug); //find how many elemnts shall be formed
console.log(rounds);
for(var i=0;i<rounds;i++){ //loop it
d.push(s.substr(0,slug)); //extract the basic initial string
//console.log(d,s,s.length) //extract the remaining string for next iteration
s = (s.length > slug) ? s.substr(slug) : s //make sure for last string less than slug value
}
console.log(d,d.length);
&#13;