Ven
Ven

Reputation: 57

Split string suing combination of string and length

I have a string like below, which i want to split using a number and "+", i tried with the below code,

Input 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

Code:

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
}

Desired Output:

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

Length by default here is 100 and which should decrease based on the string rather than increasing (help need to accomplish this)

Please help me on this with some guidance

Upvotes: 0

Views: 54

Answers (3)

RobG
RobG

Reputation: 147363

Almost the same as Nina Scholz's answer, but a little different. Starts from 0 then looks for the "+" after the next 100 characters then copies that to the result array. Starts again from the character after the "+" until the string is exhausted.

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);

Upvotes: 1

Vinod Louis
Vinod Louis

Reputation: 4876

Is this what you are looking for explanation in comments

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); 

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

You could use String#indexOf with a right start value as fromIndex to search for the next + and slice the string for the parts.

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; }

Upvotes: 1

Related Questions