HOW TO SPLIT THE STRING IN TO DIFFERENT PARTS

Manoj Bhatt

Total Post:154

Points:1086
Posted by  Manoj Bhatt
 924  View(s)
Ratings:
Rate this:
Hi Everyone! 

I want to split the string in to different parts. I will have some string which will be generated dynamically which will contains 500 characters. I want to split in to 5 parts. What i mean is i want to take 100 characters in to array[0], next 100 characters in array[1] ....

Example:

 var string = "kjfaorj.......................................................";
 array[0] = "kjfaorj..... up to 100 characters";
 array[1] = " next 100 characters ";

Thanks in advance! 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to split the string in to different parts

    Hi Manoj!


    if(str.length % 100 == 0) //If the string contains exactly 500 or 400...etc
         count = str.length / 100;
      else
        count = str.length / 100 +1; //If the string contains exactly 505 or 417...etc

    for(var i=0;i<count;i++)
      array[i] = s.substring(i,i*100-1);

    Second approach is good for dynamic string

    Thanks in advance! 

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!