Home > DeveloperSection > Forums > How to split the string in to different parts
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    May-15-2013 7:55 AM

 JavaScript JavaScript 
Ratings:


 1 Reply(s)
 675  View(s)
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! 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-15-2013 8:44 AM

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! 


Don't want to miss updates? Please click the below button!

Follow MindStick