Connecting Tech Pros Worldwide Help | Site Map

Converting string to Array

Adam
Guest
 
Posts: n/a
#1: Jul 20 '05
Hi all,

If I have a string where I know the length how do I split that into an
array based on Char position.

For example, split a string with a length of 100 into a 5 element
Array, each of 20 chars length.

Thanks in advance.
Adam
Lee
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Converting string to Array


Adam said:[color=blue]
>
>Hi all,
>
>If I have a string where I know the length how do I split that into an
>array based on Char position.
>
>For example, split a string with a length of 100 into a 5 element
>Array, each of 20 chars length.[/color]

If you know that the string is an exact multiple of 20 characters,
you can use:

var myArray=str.match(/.{20}/g);

If the last block might have fewer, but you want all of the
others to contain 20 characters, you would use:

var myArray=str.match(/.{1,20}/g);

If neither of these will work for you, give more detail about
what you need.

Adam
Guest
 
Posts: n/a
#3: Jul 20 '05

re: Converting string to Array


Lee <REM0VElbspamtrap@cox.net> wrote in message news:<bqh2t301af5@drn.newsguy.com>...[color=blue]
> Adam said:[color=green]
> >
> >Hi all,
> >
> >If I have a string where I know the length how do I split that into an
> >array based on Char position.
> >
> >For example, split a string with a length of 100 into a 5 element
> >Array, each of 20 chars length.[/color]
>
> If you know that the string is an exact multiple of 20 characters,
> you can use:
>
> var myArray=str.match(/.{20}/g);
>
> If the last block might have fewer, but you want all of the
> others to contain 20 characters, you would use:
>
> var myArray=str.match(/.{1,20}/g);
>
> If neither of these will work for you, give more detail about
> what you need.[/color]

Thanks for the Response Lee .
After a bit more investigation I realised what I was hoping for
wouldnt work for me anyway.

I have a <textarea> rows=5 cols=50.
I want to create an array with each element of the array to contain
the contents of each row from the <textarea>. I was expected to grab
the .innerText of the object and create a maximum of 5 strings each
with a length of 50.
But I realised that just because there is 50 cols in a row, there may
be more than 50 characters in that row.

Does anyone know how to create a an array of strings that contain the
contents of each row in a <textarea> object.

Thanks.
Adam
Closed Thread