Page 1 of 1

Javascript slice by character count

Posted: Tue Oct 04, 2011 4:04 am
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?

Re: Javascript slice by character count

Posted: Tue Oct 04, 2011 6:44 am
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 );
}

Re: Javascript slice by character count

Posted: Tue Oct 04, 2011 1:39 pm
by hallsofvallhalla
dang you Chris you beat me to it!!!

hehe j/k