Converting string to Array

A

Adam

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
 
L

Lee

Adam said:
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.

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.
 
A

Adam

Lee said:
Adam said:

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top