Way to split a string based on fixed length?

Discussion in 'Ruby' started by Wayne Molina, Oct 20, 2008.

  1. Wayne Molina

    Wayne Molina Guest

    This is probably a newb question but I can't seem to figure it out. I
    have a string I'm trying to parse, that is done via a fixed length
    format - it needs to be split on every 8th character in order for me to
    get a proper array list of it so I can do some additional operations on
    it.

    The string is something like this: '0000000N0000000N0000000N0000000N'
    and I need it to be an array containing something like: ['0000000N',
    '0000000N', '0000000N', '0000000N'].

    Is there a ruby method I can use to split the single string into groups
    of x number of characters, when there is no delimiter?
    --
    Posted via http://www.ruby-forum.com/.
    Wayne Molina, Oct 20, 2008
    #1
    1. Advertising

  2. Wayne Molina

    James Gray Guest

    On Oct 20, 2008, at 9:34 AM, Wayne Molina wrote:

    > This is probably a newb question but I can't seem to figure it out. I
    > have a string I'm trying to parse, that is done via a fixed length
    > format - it needs to be split on every 8th character in order for me
    > to
    > get a proper array list of it so I can do some additional operations
    > on
    > it.
    >
    > The string is something like this: '0000000N0000000N0000000N0000000N'
    > and I need it to be an array containing something like: ['0000000N',
    > '0000000N', '0000000N', '0000000N'].
    >
    > Is there a ruby method I can use to split the single string into
    > groups
    > of x number of characters, when there is no delimiter?


    Sure, you can use a regular expression for this:

    >> "0000000N0000000N0000000N0000000N".scan(/.{1,8}/m)

    => ["0000000N", "0000000N", "0000000N", "0000000N"]

    James Edward Gray II
    James Gray, Oct 20, 2008
    #2
    1. Advertising

  3. Wayne Molina

    Guest

    You can use:

    string.unpack "A8A8A4A2" # => ["0000000N", "0000000N", "0000", "00"]

    Regards, John.

    On 20/10/2008, Wayne Molina <> wrote:
    > This is probably a newb question but I can't seem to figure it out. I
    > have a string I'm trying to parse, that is done via a fixed length
    > format - it needs to be split on every 8th character in order for me to
    > get a proper array list of it so I can do some additional operations on
    > it.
    >
    > The string is something like this: '0000000N0000000N0000000N0000000N'
    > and I need it to be an array containing something like: ['0000000N',
    > '0000000N', '0000000N', '0000000N'].
    >
    > Is there a ruby method I can use to split the single string into groups
    > of x number of characters, when there is no delimiter?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    , Oct 20, 2008
    #3
  4. Wayne Molina

    Wayne Molina Guest

    Wayne Molina, Oct 20, 2008
    #4
  5. [Note: parts of this message were removed to make it a legal post.]

    I thought String#split with a regex might do it, but I'm not sure why it
    returns an array with empty strings in it. So I tried String#scan. It works,
    but since we're grouping into runs of eight characters, it returns an array
    of arrays of results. No problem, we can just use Array#flatten to take care
    of that. Here's IRB output showing the approaches:

    >> '0000000N0000000N0000000N0000000N'.split(/(\w{8})/)

    => ["", "0000000N", "", "0000000N", "", "0000000N", "", "0000000N"]
    >> '0000000N0000000N0000000N0000000N'.scan(/(\w{8})/)

    => [["0000000N"], ["0000000N"], ["0000000N"], ["0000000N"]]
    >> '0000000N0000000N0000000N0000000N'.scan(/(\w{8})/).flatten

    => ["0000000N", "0000000N", "0000000N", "0000000N"]

    Regards,
    Craig
    Craig Demyanovich, Oct 20, 2008
    #5
  6. [Note: parts of this message were removed to make it a legal post.]

    D'oh, GMail didn't update the conversation while I was replying. Anyway, you
    have some good answers.

    Craig
    Craig Demyanovich, Oct 20, 2008
    #6
  7. Wayne Molina

    Chris Causer Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Mon, Oct 20, 2008 at 4:04 PM, Craig Demyanovich
    <>wrote:

    > I thought String#split with a regex might do it, but I'm not sure why it
    > returns an array with empty strings in it.
    > [snip]
    > >> '0000000N0000000N0000000N0000000N'.split(/(\w{8})/)

    > => ["", "0000000N", "", "0000000N", "", "0000000N", "", "0000000N"]



    Going a bit off topic here, but I suspect the reason split adds empty
    strings is because it is matching the 8 characters, splitting there, but
    because you are capturing them, puts the delimiter back in the array it as
    well. The gap between the 8 characters is nothing, thus "". I guess that if
    you split(/\w{8}/) you'll get nothing because there will be nothing left
    after removing the delimeter (unless the string isn't of length 8n, n is an
    integer.)
    Chris Causer, Oct 20, 2008
    #7
  8. Wayne Molina

    Adam Penny Guest

    Craig Demyanovich wrote:
    > I thought String#split with a regex might do it, but I'm not sure why it
    > returns an array with empty strings in it. So I tried String#scan. It
    > works,


    Don't quote me on this, but I think that would be because String#split
    had a regex to define the character/group of characters that you're
    interested in, so if you're defining the split as any old 8 characters
    then that doesn't leave an awful lot!
    --
    Posted via http://www.ruby-forum.com/.
    Adam Penny, Oct 21, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Greschke

    Fixed length lists from .split()?

    Bob Greschke, Jan 26, 2007, in forum: Python
    Replies:
    7
    Views:
    297
    George Sakkis
    Feb 2, 2007
  2. Victor Bazarov
    Replies:
    2
    Views:
    699
    Alf P. Steinbach
    May 3, 2010
  3. cpp4ever
    Replies:
    2
    Views:
    886
    cpp4ever
    May 4, 2010
  4. Sam Kong
    Replies:
    5
    Views:
    236
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    602
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page