4.4 Modifying Strings

You can alter the contents of a mutable string via operations described in this section. See Mutability.

The most basic way to alter the contents of an existing string is with aset (see Functions that Operate on Arrays). (aset string idx char) stores char into string at character index idx. It will automatically convert a pure-ASCII string to a multibyte string (see Text Representations) if needed, but we recommend to always make sure string is multibyte (e.g., by using string-to-multibyte, see Converting Text Representations), if char is a non-ASCII character, not a raw byte.

A more powerful function is store-substring:

Function: store-substring string idx obj

This function alters part of the contents of the specified string, by storing obj starting at character index idx. The argument obj may be either a character (in which case the function behaves exactly as aset) or a (smaller) string. If obj is a multibyte string, we recommend to make sure string is also multibyte, even if it’s pure-ASCII.

Since it is impossible to change the number of characters in an existing string, it is an error if obj consists of more characters than would fit in string starting at character index idx.

To clear out a string that contained a password, use clear-string:

Function: clear-string string

This makes string a unibyte string and clears its contents to zeros. It may also change string’s length.