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 "}}