6
$\begingroup$

Possible Duplicate:
Partition string into chunks

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

"ABCDEabcde1234"

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.

$\endgroup$
8
  • 4
    $\begingroup$ I see a multitude of answers coming! Perfect for non-experts. $\endgroup$ Commented Oct 3, 2012 at 18:04
  • $\begingroup$ @IstvánZachar it may very well give experts a run for their money, too. As there are a lot of choices. $\endgroup$
    – rcollyer
    Commented Oct 3, 2012 at 18:10
  • $\begingroup$ As a first hint: look up Partition[] and Characters[]. $\endgroup$ Commented Oct 3, 2012 at 18:11
  • $\begingroup$ I will restrain myself for an hour... $\endgroup$ Commented Oct 3, 2012 at 18:12
  • 1
    $\begingroup$ Related question (Partition string into chunks) $\endgroup$
    – user1066
    Commented Oct 3, 2012 at 18:56

2 Answers 2

8
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ The regex equivalent: StringCases[str, RegularExpression[".{1," <> ToString[k] <> "}"]] $\endgroup$ Commented Oct 3, 2012 at 18:42
4
$\begingroup$

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.

$\endgroup$
6
  • 2
    $\begingroup$ ...the non-expert answer. $\endgroup$
    – kale
    Commented Oct 3, 2012 at 18:20
  • $\begingroup$ StringJoin @@@ Partition[Characters[str], n] works just as well. $\endgroup$ Commented Oct 3, 2012 at 18:21
  • 1
    $\begingroup$ @J.M., only if you have a multiple of the partition size. Right? Your way drops characters at the end in some instances. $\endgroup$
    – kale
    Commented Oct 3, 2012 at 18:22
  • $\begingroup$ @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"}. $\endgroup$
    – kale
    Commented Oct 3, 2012 at 18:25
  • $\begingroup$ True. I suppose that depends on whether the length of OP's strings would always be commensurate with the partition length... $\endgroup$ Commented Oct 3, 2012 at 18:28

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