strings.icn: Procedures for manipulating strings

procedure cat:             concatenate strings
procedure charcnt:         character count
procedure collate:         string collation
procedure comb:            character combinations
procedure compress:        character compression
procedure coprefix:        find common prefix of strings
procedure cosuffix:        find common suffix of strings
procedure csort:           lexically ordered characters
procedure decollate:       string decollation
procedure deletec:         delete characters
procedure deletes:         delete string
procedure diffcnt:         number of different characters
procedure extend:          extend string
procedure fchars:          characters in order of frequency
procedure interleave:      interleave strings
procedure ispal:           test for palindrome
procedure maxlen:          maximum string length
procedure meander:         meandering strings
procedure multicoll:       collate strings in list
procedure ochars:          first appearance unique characters
procedure odd_even:        odd-even numerical string
procedure palins:          palindromes
procedure permutes:        generate string permutations
procedure pretrim:         pre-trim string
procedure reflect:         string reflection
procedure replace:         string replacement
procedure replacem:        multiple string replacement
procedure replc:           replicate characters
procedure rotate:          string rotation
procedure schars:          lexical unique characters
procedure scramble:        scramble string
procedure selectp:         select characters
procedure slugs:           generate s in chunks of size <= n
procedure starseq:         closure sequence
procedure strcnt:          substring count
procedure substrings:      generate substrings
procedure transpose:       transpose characters
procedure words:           generate words from string

link strings
May 8, 2002; Ralph E. Griswold
This file is in the public domain.

These procedures perform operations on strings.

     cat(s1, s2, ...)   concatenates an  arbitrary number of strings.

     charcnt(s, c)      returns the number of instances of characters in
                        c in s.

     collate(s1, s2)    collates the characters of s1 and s2.  For example,
                            collate("abc", "def")
                        produces "adbecf".

     comb(s, i)         generates the combinations of characters from s
                        taken i at a time.

     compress(s, c)     compresses consecutive occurrences of charac-
                        ters in c that occur in s; c defaults to &cset.

     coprefix(s1, s2, ...)
                        produces the common prefix of its arguments:
                        the longest initial substring shared by all,
                        which may be the empty string.

     cosuffix(s1, s2, ...)
                        produces the common suffix of its arguments:
                        the longest trailing substring shared by all,
                        which may be the empty string.

     csort(s)           produces the characters of s in lexical order.

     decollate(s, i)    produces a string consisting of every other
                        character of s. If i is odd, the odd-numbered
                        characters are selected, while if i is even,
                        the even-numbered characters are selected.
                        The default value of i is 1.

     deletec(s, c)      deletes occurrences of characters in c from s.

     deletep(s, L)      deletes all characters at positions specified in
                        L.

     deletes(s1, s2)    deletes occurrences of s2 in s1.

     diffcnt(s)         returns count of the number of different
                        characters in s.

     extend(s, n)       replicates s to length n.

     fchars(s)          returns characters of s in order of decreasing
                        frequency

     interleave(s1, s2) interleaves characters s2 extended to the length
                        of s1 with s1.

     ispal(s)           succeeds and returns s if s is a palindrome

     maxlen(L, p)       returns the length of the longest string in L.
                        If p is given, it is applied to each string as
                        as a "length" procedure.  The default for p is
                        proc("*", 1).

     meander(s, n)      produces a "meandering" string that contains all
                        n-tuples of characters of s.

     multicoll(L)       returns the collation of the strings in L.

     ochars(s)          produces the unique characters of s in the order
                        that they first appear in s.

     odd_even(s)        inserts values in a numerical string so that
                        adjacent digits follow an odd-even pattern.

     palins(s, n)       generates all the n-character palindromes from the
                        characters in s.

     permutes(s)        generates all the permutations of the string s.

     pretrim(s, c)      trims characters from beginning of s.

     reflect(s1, i, s2)
                        returns s1 concatenated s2 and the reversal of s1
                        to produce a palindroid; the values of i
                        determine "end conditions" for the reversal:

                             0       pattern palindrome; the default
                             1       pattern palindrome with center duplicated
                             2       true palindrome with center not duplicated
                             3       true palindrome with center duplicated

                        s2 defaults to the empty string, in which case the
                        result is a full palindrome

     replace(s1, s2, s3)
                        replaces all occurrences of s2 in s1 by s3; fails
                        if s2 is null.

     replacem(s, ...)   performs multiple replacements in the style of
                        of replace(), where multiple argument pairs
                        may be given, as in

                             replacem(s, "a", "bc", "d", "cd")

                        which replaced all "a"s by "bc"s and all
                        "d"s by "cd"s.  Replacements are performed
                        one after another, not in parallel.

     replc(s, L)        replicates each character of c by the amount
                        given by the values in L.

     rotate(s, i)       rotates s i characters to the left (negative i
                        produces rotation to the right); the default
                        value of i is 1.

     schars(s)          produces the unique characters of s in lexical
                        order.

     scramble(s)        scrambles (shuffles) the characters of s randomly.

     selectp(s, L)      selects characters of s that are at positions
                        given in L.

     slugs(s, n, c)     generates column-sized chunks (length <= n)
                        of string s broken at spans of cset c.

                        Defaults:    n       80
                                     c       ' \t\r\n\v\f'

                        Example:  every write(">  ", slugs(msg, 50))

     starseq(s)         sequence consisting of the closure of s
                        starting with the empty string and continuing
                        in lexical order as given in s

     strcnt(s1, s2)     produces a count of the number of non-overlapping
                        times s1 occurs in s2; fails is s1 is null

     substrings(s, i, j)
                        generates all the substrings of s with lengths
                        from i to j, inclusive; i defaults to 1, j
                        to *s

     transpose(s1, s2, s3)
                        transposes s1 according to label s2 and
                        transposition s3.

     words(s, c)        generates the "words" from the string s that
                        are separated by characters from the cset
                        c, which defaults to ' \t\r\n\v\f'.

Source code | Program Library Page | Icon Home Page