Re: Partition(divid string to substring
- To: mathgroup at smc.vnet.net
- Subject: [mg60968] Re: Partition(divid string to substring
- From: Peter Pein <petsie at dordos.net>
- Date: Wed, 5 Oct 2005 02:28:14 -0400 (EDT)
- References: <dht4h7$hnr$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Sara schrieb:
> I have to canstruct a method stringPartition[s,n] that divides a string s into substrings of length n. The last string shall be filled with spaces to the specified length.and I have too use StringTake. But i have done it Like that:
> stringPartition[s_, n_] := Module[
> {},
> emptyString = Mod[StringLength[s], n];
> If[emptyString â� 0, empt = n - emptyString];
> str1 := " ";
> str2 := "";
> For[i = 1, i � empt, i++,
> str2 = str2 <> str1];
> str = s <> str2;
> strCode = ToCharacterCode[str, "UTF8"];
> textPartition = Partition[strCode, n];
> textCode = FromCharacterCode[textPartition, "UTF8"]
> ]
> and its work, but I dont know how to use Stringtake instead (toCharacterCode), I dont want to use Toharachtercode.
> Thanks
>
Hi Sara, I did only read your first posting before answering.
You have to use StringTake? (Homework?)
Well, append n-1 Spaces to your input string and divide:
stringPartition[s_, n_] :=
StringTake[Nest[# <> " " &, s, n - 1],
{n(# - 1) + 1, n #}] & /@ Range[Ceiling[StringLength[s]/n]]
stringPartition["A string", 3]//InputForm
-->
{"A s", "tri", "ng "}
Peter