Converting string to Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam

    Converting string to Array

    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

    #2
    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.mat ch(/.{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.mat ch(/.{1,20}/g);

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

    Comment

    • Adam

      #3
      Re: Converting string to Array

      Lee <REM0VElbspamtr ap@cox.net> wrote in message news:<bqh2t301a f5@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.mat ch(/.{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.mat ch(/.{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

      Comment

      Working...