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?
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:
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!
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.)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.