Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Re: Partition string into length n chunks

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126538] Re: Partition string into length n chunks
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Fri, 18 May 2012 05:23:22 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jp2bvf$506$1@smc.vnet.net>

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