Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10122

Re: Partition string into length n chunks

From David Bailey <dave@removedbailey.co.uk>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Partition string into length n chunks
Date 2012-05-18 09:24 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jp54g3$gm1$1@smc.vnet.net> (permalink)
References <jp2bvf$506$1@smc.vnet.net>

Show all headers | View raw


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

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Partition string into length n chunks David G <daggillies@gmail.com> - 2012-05-17 08:13 +0000
  Re: Partition string into length n chunks David Bailey <dave@removedbailey.co.uk> - 2012-05-18 09:24 +0000
  Re: Partition string into length n chunks Szabolcs Horvát <szhorvat@gmail.com> - 2012-05-18 09:27 +0000

csiph-web