MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Partition string into length n chunks


On 17/05/2012 09:13, David G wrote:
> I am sure this is trivially simple, but how do I partition a string into non-overlapping length n chunks? The best I could come up with is chunk[s_, n_] := StringJoin[#]&  /@ Partition[Characters[s], n] but this seems overly long-winded.
>

I suspect this is almost as good as you will get, but for speed I would 
avoid the pure function:

chunks[s_,n_]:=Map[StringJoin,Partition[Characters[s], n]];

Note that stray characters on the end of the string are just lost.

David Bailey
http://www.dbaileyconsultancy.co.uk



  • Prev by Date: Re: Partition string into length n chunks
  • Next by Date: Re: Coefficient
  • Previous by thread: Re: Partition string into length n chunks
  • Next by thread: Coefficient