Javascript slice by character count

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Ravinos
Posts: 281
Joined: Tue Sep 15, 2009 4:14 pm

Javascript slice by character count

Post by Ravinos »

How would one go about splitting up a block of text say 1000 characters long into 200 character segments and making each segment a div?
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Javascript slice by character count

Post by Chris »

You could split the string with the split method:

Code: Select all

string.split( seperator )
equivelent in php:

Code: Select all

$array = explode( seperator, $string );
 
or with the string.substr() method:

Code: Select all

string1 = string.substr(0,200)
string2 = string.substr(200,200)
string3 = string.substr(400,200);
You might want to loop it though:

Code: Select all

length = string.length;
loops = (length/200)
strings = []
for( var i = 0; i < loops; i++ )
{
    strings[i] = string.substr( (i*200), 200 );
}
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Javascript slice by character count

Post by hallsofvallhalla »

dang you Chris you beat me to it!!!

hehe j/k
Post Reply

Return to “Coding”