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: [mg60961] Re: [mg60940] Partition(divid string to substring
  • From: Igor Antonio <igora at wolf-ram.com>
  • Date: Wed, 5 Oct 2005 02:28:00 -0400 (EDT)
  • Organization: Wolfram Research, Inc.
  • References: <200510040525.BAA17959@smc.vnet.net>
  • Reply-to: igora at wolf-ram.com
  • Sender: owner-wri-mathgroup at wolfram.com

Sara wrote:
> 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

Sara,

You *have* to use StringTake?  Sounds like a homework assignment, in 
which case figuring out what the code below does "is left as an exercise 
to the reader." :-)

It's probably not the cleanest code, but just what I came up with last 
night.  The idea is to create a list of the positions that follow the 
syntax for the second argument of StringTake:

StringTake["string", {m, n}] a gives characters m through n in "string".

Once you have that list of pairs, it's just a matter of mapping your 
StringTake function over that list + dealing with your requirement of 
padding the last one with spaces.

myFunc[str_String, n_Integer] :=
  Module[{remainder, div, posList, lastNum, lastSubStr},
    remainder = Mod[StringLength[str], n];
    div = Floor[StringLength[str]/n];
    posList = NestList[#1 + n & , 0, div];
    posList = ({1, 0} + #1 & ) /@ Partition[posList, 2, 1, {1, -1}];
      If[remainder == 0, (StringTake[str, #1] & ) /@ posList,
        lastNum = Last[Last[posList]] + 1;
        lastSubStr = StringTake[str, {lastNum, StringLength[str]}];
        lastSubStr = StringInsert[lastSubStr, " ",
        NestList[Plus, StringLength[lastSubStr] + 1, n - 
StringLength[lastSubStr]]];
        Join[(StringTake[str, #1] & ) /@ posList, {lastSubStr}]]]



(myFunc["thisIsAStringThatsVeryLong", #1] & ) /@ Range[26]

gives

{
{"t", "h", "i", "s", "I", "s", "A", "S", "t", "r", "i", "n", "g", "T", 
"h", "a", "t", "s", "V", "e", "r", "y", "L", "o", "n", "g"},
{"th", "is", "Is", "AS", "tr", "in", "gT", "ha", "ts", "Ve", "ry", "Lo", 
"ng"},
{"thi", "sIs", "ASt", "rin", "gTh", "ats", "Ver", "yLo", "ng  "}, 
{"this", "IsAS", "trin", "gTha", "tsVe", "ryLo", "ng   "},
{"thisI", "sAStr", "ingTh", "atsVe", "ryLon", "g     "},
{"thisIs", "AStrin", "gThats", "VeryLo", "ng     "},
{"thisIsA", "StringT", "hatsVer", "yLong   "},
{"thisIsAS", "tringTha", "tsVeryLo", "ng       "},
{"thisIsASt", "ringThats", "VeryLong  "},
{"thisIsAStr", "ingThatsVe", "ryLong     "},
{"thisIsAStri", "ngThatsVery", "Long        "},
{"thisIsAStrin", "gThatsVeryLo", "ng           "},
{"thisIsAString", "ThatsVeryLong"},
{"thisIsAStringT", "hatsVeryLong   "},
{"thisIsAStringTh", "atsVeryLong     "},
{"thisIsAStringTha", "tsVeryLong       "},
{"thisIsAStringThat", "sVeryLong         "},
{"thisIsAStringThats", "VeryLong           "},
{"thisIsAStringThatsV", "eryLong             "},
{"thisIsAStringThatsVe", "ryLong               "}, 
{"thisIsAStringThatsVer", "yLong                 "},
{"thisIsAStringThatsVery", "Long                   "},
{"thisIsAStringThatsVeryL", "ong                     "},
{"thisIsAStringThatsVeryLo", "ng                       "}, 
{"thisIsAStringThatsVeryLon", "g                         "}, 
{"thisIsAStringThatsVeryLong"}

}


-- 


Igor C. Antonio
Wolfram Research, Inc.
http://www.wolfram.com

To email me personally, remove the dash.


  • Prev by Date: Re: "Mathematica in Education and Research"
  • Next by Date: NET/Link return array from C++
  • Previous by thread: Partition(divid string to substring
  • Next by thread: Re: Partition(divid string to substring