Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Partition(divid string to substring

  • To: mathgroup at smc.vnet.net
  • Subject: [mg60971] Re: Partition(divid string to substring
  • From: "dkr" <dkrjeg at adelphia.net>
  • Date: Wed, 5 Oct 2005 02:28:25 -0400 (EDT)
  • References: <dht4h7$hnr$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

If you must use StringTake, try:

In[1]:=
stringPartition[s_,n_?IntegerQ/;n>0]:=Module[{len=StringLength[s],m,x},
      m=Mod[len,n];
      x:={StringJoin[s,StringJoin[Table[" ",{n-len}]]]}/;len<n;
      x:=StringTake[s,#]&/@NestList[Plus[#,{n,n}]&,{1,n},(len/n)-1]/;
          m==0;
      x:=Join[
          StringTake[s,#]&/@

NestList[Plus[#,{n,n}]&,{1,n},Quotient[len,n]-1],{StringJoin[
              StringTake[s,-m],StringJoin@Table[" ",{n-m}]]}];
      x
      ];
In[4]:=
str="abcdefghij";
stringPartition[str,#]&/@Range[1,12]//InputForm
Out[5]//InputForm=
{{"a", "b", "c", "d", "e", "f", "g", "h", "i",
  "j"}, {"ab", "cd", "ef", "gh", "ij"},
 {"abc", "def", "ghi", "j  "},
 {"abcd", "efgh", "ij  "}, {"abcde", "fghij"},
 {"abcdef", "ghij  "}, {"abcdefg", "hij    "},
 {"abcdefgh", "ij      "}, {"abcdefghi",
  "j        "}, {"abcdefghij"}, {"abcdefghij "}, 
 {"abcdefghij  "}}


  • Prev by Date: Re: Partition(divid string to substring
  • Next by Date: Re: the faster way to find repeated sublists
  • Previous by thread: Re: Partition(divid string to substring
  • Next by thread: Re: Partition(divid string to substring