Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Partition string into chunks

How can I split a string into sub strings of length n? For example I have a string


I would like to split it into

{"AB", "CD", "Ea", "bc", "de", "12", "34"}

How can I achieve this? I have looked at the StringSplit[] documentation but that seems like it only works for splitting a string by character, not length.

share|improve this question

marked as duplicate by J. M. Oct 3 '12 at 18:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

I see a multitude of answers coming! Perfect for non-experts. – István Zachar Oct 3 '12 at 18:04
@IstvánZachar it may very well give experts a run for their money, too. As there are a lot of choices. – rcollyer Oct 3 '12 at 18:10
As a first hint: look up Partition[] and Characters[]. – J. M. Oct 3 '12 at 18:11
I will restrain myself for an hour... – Sjoerd C. de Vries Oct 3 '12 at 18:12
Related question (Partition string into chunks) – user 106 Oct 3 '12 at 18:56

I think @OleksandrR once suggested to me in chat this solution

stringPartition[s_, n_]:=StringCases[s, Repeated[_, n]]

The key is that the option Overlaps defaults to False. If you want to be on the safe side you can add it explicitly

If you want the last part to be removed if it doesn't have n elements, you could use Repeated[_, {n}] instead.

share|improve this answer
The regex equivalent: StringCases[str, RegularExpression[".{1," <> ToString[k] <> "}"]] – J. M. Oct 3 '12 at 18:42

Try this:

splitstring[String : str_, n_] := StringJoin @@@ Partition[Characters[str], n, n, 1, {}]

In[116]:= splitstring["ABCDEabcde1234", 2]

Out[116]= {"AB", "CD", "Ea", "bc", "de", "12", "34"}

As J.M. notes below:

splitstring[String : str_, n_] := StringJoin @@@ Partition[Characters[str], n]

works just as well, but will drop characters at the end if your string length isn't a multiple of n.

So choose based on your need.

share|improve this answer
...the non-expert answer. – kale Oct 3 '12 at 18:20
StringJoin @@@ Partition[Characters[str], n] works just as well. – J. M. Oct 3 '12 at 18:21
@J.M., only if you have a multiple of the partition size. Right? Your way drops characters at the end in some instances. – kale Oct 3 '12 at 18:22
@J.M. If the string was a character longer, it would be lost with a partition size of two. splitstring["ABCDEabcde12345", 2] outputs {"AB", "CD", "Ea", "bc", "de", "12", "34"}. – kale Oct 3 '12 at 18:25
True. I suppose that depends on whether the length of OP's strings would always be commensurate with the partition length... – J. M. Oct 3 '12 at 18:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.