3
$\begingroup$

I'm trying to extract every 21st character from this text, s (given below), to create new strings of all 1st characters, 2nd characters, etc.

I have already separated the long string into substrings of 21 characters each using

    splitstring[String : str_, n_] := 
    StringJoin @@@ Partition[Characters[str], n, n, 1, {}]

giving:

    {"ALJJJEAQZJMZKOZDKEHBL", "XLPXNEHZCSEJVVLWHTUDJ", \
    "WFYXKKMWNNTNPHDTMGIOP", "OOSYPXGTLOHOPHTDHBHWO", \
    "MWGSKXSTNNEYSQHRSGPKP", "CJBNVIYCZHIVPFSWCKFPJ", \
    "OZQLNGPTLCIALHMBIGUOP", "ESYNDGACTURTALHLSGFBR", \
    "LPRMYKFQFXTEEZQHIUMOC", "CLSUIKWYLRPRRJZWCKUOW", \
    "WJLLVYEJXNIEMDQYQTDFC", "HJQLYKFQJTKUCICIRKOWX", \
    "PHLWYCCDRKVPAHCYPZFRL", "CNYBIOEWWGHQQBCDHGPRW", \
    "WHWIUOQTYOJLHGLFRTTVL", "CQCIUEHZYJKJEWHOVMYOM", \
    "JOBTIHCOSGCZVZJFEYHAC", "JQCQFSTWLOHYPXZDHBHTW", \
    "TLGIUJIWEXSJGKLTMKAFF", "PYGYICGSPRPLPOZNKPUSH", \
    "NSMQCGPWHILVXLZARZLIS", "POBDNEXJYMESQDTAQWKWL", \
    "WPBIDCCJXKHQCOTAJXBVZ", "WWDNCCCCWACQHZJREEHRO", \
    "LQSSRCVPFHCJCGLURBHFM", "POYKFKWRHKVGLLSYRTISC", \
    "ESYKXCEYFNSAHUHYIBJVL", "WZLKPJHQJEQQVHNHSEHTO", \
    "TNFJCRMZCCIEXKSALTMFP", "YTLGCORTMRIAILOISWKRJ", \
    "DTMRXGVYHEHRSIPRIWKOX", "SLBQVJHSZTMZMIHRAFFTJ", \
    "CTMSYGADQEHQXUZIQHLZJ", "OOGRZGVHLKPTIUEHHKHKT", \
    "CHCNCMMYLIMBWWHNKXBQP", "DEWQCIVZRNETVVPFCVYST", \
    "RTYZYEPWZTMQHLNHSGPRO", "TNFJCRRLNNPRHGYARRJVW", \
    "HJBIFTAPWRVUCZODYYLGF", "COBDWKMDTGJCEBDTVRDRO", \
    "HJQLPLVHJYKNJSLGEBZDL", "OOWKROWOOOJRXKRAMYMMM", \
    "FOBFTGTSLHIGLQLWVVLTL", "TDYBEGVNJLEAQDPRQXKRH", \
    "WOGIUCPLCJEAJBYGLTSCY", "OCUDECCQCURAELOAPEHKP", \
    "YJBIVOPWZTEVHJHNEYNMX", "CFSHYKPPWCGUMEWYKNHZW", \
    "JQSWCRANSOALVIJLPRZDL", "YOFDJVCDHTAVAHTRMTBHP", "RJZBIOEOSCR"}

How can I make strings of all the first characters, second characters, etc.?

    s=ALJJJEAQZJMZKOZDKEHBLXLPXNEHZCSEJVVLWHTUDJWFYXKKMWNNTNPHDTMGIOPOOSYPXG\
    TLOHOPHTDHBHWOMWGSKXSTNNEYSQHRSGPKPCJBNVIYCZHIVPFSWCKFPJOZQLNGPTLCIALH\
    MBIGUOPESYNDGACTURTALHLSGFBRLPRMYKFQFXTEEZQHIUMOCCLSUIKWYLRPRRJZWCKUOW\
    WJLLVYEJXNIEMDQYQTDFCHJQLYKFQJTKUCICIRKOWXPHLWYCCDRKVPAHCYPZFRLCNYBIOE\
    WWGHQQBCDHGPRWWHWIUOQTYOJLHGLFRTTVLCQCIUEHZYJKJEWHOVMYOMJOBTIHCOSGCZVZ\
    JFEYHACJQCQFSTWLOHYPXZDHBHTWTLGIUJIWEXSJGKLTMKAFFPYGYICGSPRPLPOZNKPUSH\
    NSMQCGPWHILVXLZARZLISPOBDNEXJYMESQDTAQWKWLWPBIDCCJXKHQCOTAJXBVZWWDNCCC\
    CWACQHZJREEHROLQSSRCVPFHCJCGLURBHFMPOYKFKWRHKVGLLSYRTISCESYKXCEYFNSAHU\
    HYIBJVLWZLKPJHQJEQQVHNHSEHTOTNFJCRMZCCIEXKSALTMFPYTLGCORTMRIAILOISWKRJ\
    DTMRXGVYHEHRSIPRIWKOXSLBQVJHSZTMZMIHRAFFTJCTMSYGADQEHQXUZIQHLZJOOGRZGV\
    HLKPTIUEHHKHKTCHCNCMMYLIMBWWHNKXBQPDEWQCIVZRNETVVPFCVYSTRTYZYEPWZTMQHL\
    NHSGPROTNFJCRRLNNPRHGYARRJVWHJBIFTAPWRVUCZODYYLGFCOBDWKMDTGJCEBDTVRDRO\
    HJQLPLVHJYKNJSLGEBZDLOOWKROWOOOJRXKRAMYMMMFOBFTGTSLHIGLQLWVVLTLTDYBEGV\
    NJLEAQDPRQXKRHWOGIUCPLCJEAJBYGLTSCYOCUDECCQCURAELOAPEHKPYJBIVOPWZTEVHJ\
    HNEYNMXCFSHYKPPWCGUMEWYKNHZWJQSWCRANSOALVIJLPRZDLYOFDJVCDHTAVAHTRMTBHP\
    RJZBIOEOSCR
$\endgroup$

3 Answers 3

7
$\begingroup$

StringTake and Span would be useful.

For example, to get the second characters:

StringTake[s, 2 ;; ;; 21]
(* "LLFOWJZSPLJJHNHQOQLYSOPWQOSZNTTLTOHETNJOJOODOCJFQOJ" *)

To get all of the strings:

StringTake[s, Array[# ;; ;; 21&, 21]]

Another approach would be a combination of Characters and Part:

StringJoin[Characters[s][[2 ;; ;; 21]]]
(* "LLFOWJZSPLJJHNHQOQLYSOPWQOSZNTTLTOHETNJOJOODOCJFQOJ" *)

Again, to get all of the strings:

Array[StringJoin[Characters[s][[# ;; ;; 21]]]&, 21]
$\endgroup$
1
$\begingroup$

The last substring does not contain 21 characters but you can use the padded form of Partition to get sublist of equal length with padding. Then it is just a Transpose and StringJoin to the result. I use "&" for padding which you can substitute for any character.

StringJoin@Flatten@Transpose@Partition[Characters[s], 21, 21, {1, 1}, "&"]

Hope this helps.

$\endgroup$
1
$\begingroup$
StringJoin /@ Transpose[Characters @ StringPartition[s,21]]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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