Next: String Input and Output, Previous: stringproc, Up: stringproc [Contents][Index]

### 91.1 Introduction to String Processing

The package `stringproc` contains functions for processing strings and characters including formatting, encoding and data streams. This package is completed by some tools for cryptography, e.g. base64 and hash functions.

It can be directly loaded via `load("stringproc")` or automatically by using one of its functions.

For questions and bug reports please contact the author. The following command prints his e-mail-address.

`printf(true, "~{~a~}@gmail.com", split(sdowncase("Volker van Nek")))$`

A string is constructed by typing e.g. `"Text"`. When the option variable `stringdisp` is set to `false`, which is the default, the double quotes won’t be printed. stringp is a test, if an object is a string.

    (%i1) str: "Text";
    (%o1)                         Text
    (%i2) stringp(str);
    (%o2)                         true

Characters are represented by a string of length 1. charp is the corresponding test.

    (%i1) char: "e";
    (%o1)                           e
    (%i2) charp(char);
    (%o2)                         true

In Maxima position indices in strings are like in list 1-indexed which results to the following consistency.

    (%i1) is(charat("Lisp",1) = charlist("Lisp")[1]);
    (%o1)                         true

A string may contain Maxima expressions. These can be parsed with parse_string.

    (%i1) map(parse_string, ["42" ,"sqrt(2)", "%pi"]);
    (%o1)                   [42, sqrt(2), %pi]
    (%i2) map('float, %);
    (%o2)        [42.0, 1.414213562373095, 3.141592653589793]

Strings can be processed as characters or in binary form as octets. Functions for conversions are string_to_octets and octets_to_string. Usable encodings depend on the platform, the application and the underlying Lisp. (The following shows Maxima in GNU/Linux, compiled with SBCL.)

    (%i1) obase: 16.$
    (%i2) string_to_octets("$£€", "cp1252");
    (%o2)                     [24, 0A3, 80]
    (%i3) string_to_octets("$£€", "utf-8");
    (%o3)               [24, 0C2, 0A3, 0E2, 82, 0AC]

Strings may be written to character streams or as octets to binary streams. The following example demonstrates file in and output of characters.

openw returns an output stream to a file, printf writes formatted to that file and by e.g. close all characters contained in the stream are written to the file.

    (%i1) s: openw("file.txt");
    (%o1)                #<output stream file.txt>
    (%i2) printf(s, "~%~d ~f ~a ~a ~f ~e ~a~%",
    42, 1.234, sqrt(2), %pi, 1.0e-2, 1.0e-2, 1.0b-2)$
    (%i3) close(s)$

openr then returns an input stream from the previously used file and readline returns the line read as a string. The string may be tokenized by e.g. split or tokens and finally parsed by parse_string.

    (%i4) s: openr("file.txt");
    (%o4)                 #<input stream file.txt>
    (%i5) readline(s);
    (%o5)          42 1.234 sqrt(2) %pi 0.01 1.0E-2 1.0b-2
    (%i6) map(parse_string, split(%));
    (%o6)       [42, 1.234, sqrt(2), %pi, 0.01, 0.01, 1.0b-2]
    (%i7) close(s)$

Categories: Strings · Share packages · Package stringproc ·

Next: String Input and Output, Previous: stringproc, Up: stringproc [Contents][Index]