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


Groups > comp.soft-sys.math.mathematica > #10110 > unrolled thread

Partition string into length n chunks

Started byDavid G <daggillies@gmail.com>
First post2012-05-17 08:13 +0000
Last post2012-05-18 09:27 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  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

#10110 — Partition string into length n chunks

FromDavid G <daggillies@gmail.com>
Date2012-05-17 08:13 +0000
SubjectPartition string into length n chunks
Message-ID<jp2bvf$506$1@smc.vnet.net>
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.

[toc] | [next] | [standalone]


#10122

FromDavid Bailey <dave@removedbailey.co.uk>
Date2012-05-18 09:24 +0000
Message-ID<jp54g3$gm1$1@smc.vnet.net>
In reply to#10110
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

[toc] | [prev] | [next] | [standalone]


#10128

FromSzabolcs Horvát <szhorvat@gmail.com>
Date2012-05-18 09:27 +0000
Message-ID<jp54lq$goj$1@smc.vnet.net>
In reply to#10110
On 2012.05.17. 10: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.
>

This question has been answered here:

http://mathematica.stackexchange.com/questions/5644/partition-string-into-chunks

-- 
Szabolcs Horvát
Visit Mathematica.SE:  http://mathematica.stackexchange.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web