By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

Converting string to Array

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

Jul 20 '05 #2

P: n/a
Lee <RE**************@cox.net> wrote in message news:<bq*********@drn.newsguy.com>...
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.


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
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.