Skip to main content

4.3 Creating Strings

The following functions create strings, either from scratch, or by putting strings together, or by taking them apart. (For functions that create strings based on the modified contents of other strings, like string-replace and replace-regexp-in-string, see Search and Replace.)

function make-string count character \&optional multibyte​

This function returns a string made up of count repetitions of character. If count is negative, an error is signaled.

(make-string 5 ?x)
β‡’ "xxxxx"
(make-string 0 ?x)
β‡’ ""

Normally, if character is an ASCII character, the result is a unibyte string. But if the optional argument multibyte is non-nil, the function will produce a multibyte string instead. This is useful when you later need to concatenate the result with non-ASCII strings or replace some of its characters with non-ASCII characters.

Other functions to compare with this one include make-vector (see Vectors) and make-list (see Building Lists).

function string \&rest characters​

This returns a string containing the characters characters.

(string ?a ?b ?c)
β‡’ "abc"

function substring string \&optional start end​

This function returns a new string which consists of those characters from string in the range from (and including) the character at the index start up to (but excluding) the character at the index end. The first character is at index zero. With one argument, this function just copies string.

(substring "abcdefg" 0 3)
β‡’ "abc"

In the above example, the index for β€˜a’ is 0, the index for β€˜b’ is 1, and the index for β€˜c’ is 2. The index 3β€”which is the fourth character in the stringβ€”marks the character position up to which the substring is copied. Thus, β€˜abc’ is copied from the string "abcdefg".

A negative number counts from the end of the string, so that -1 signifies the index of the last character of the string. For example:

(substring "abcdefg" -3 -1)
β‡’ "ef"

In this example, the index for β€˜e’ is -3, the index for β€˜f’ is -2, and the index for β€˜g’ is -1. Therefore, β€˜e’ and β€˜f’ are included, and β€˜g’ is excluded.

When nil is used for end, it stands for the length of the string. Thus,

(substring "abcdefg" -3 nil)
β‡’ "efg"

Omitting the argument end is equivalent to specifying nil. It follows that (substring string 0) returns a copy of all of string.

(substring "abcdefg" 0)
β‡’ "abcdefg"

But we recommend copy-sequence for this purpose (see Sequence Functions).

If the characters copied from string have text properties, the properties are copied into the new string also. See Text Properties.

substring also accepts a vector for the first argument. For example:

(substring [a b (c) "d"] 1 3)
β‡’ [b (c)]

A wrong-type-argument error is signaled if start is not an integer or if end is neither an integer nor nil. An args-out-of-range error is signaled if start indicates a character following end, or if either integer is out of range for string.

Contrast this function with buffer-substring (see Buffer Contents), which returns a string containing a portion of the text in the current buffer. The beginning of a string is at index 0, but the beginning of a buffer is at index 1.

function substring-no-properties string \&optional start end​

This works like substring but discards all text properties from the value. Also, start may be omitted or nil, which is equivalent to 0. Thus, (substring-no-propertiesΒ string) returns a copy of string, with all text properties removed.

function concat \&rest sequences​

This function returns a string consisting of the characters in the arguments passed to it (along with their text properties, if any). The arguments may be strings, lists of numbers, or vectors of numbers; they are not themselves changed. If concat receives no arguments, it returns an empty string.

(concat "abc" "-def")
β‡’ "abc-def"
(concat "abc" (list 120 121) [122])
β‡’ "abcxyz"
;; nil is an empty sequence.
(concat "abc" nil "-def")
β‡’ "abc-def"
(concat "The " "quick brown " "fox.")
β‡’ "The quick brown fox."
(concat)
β‡’ ""

This function does not always allocate a new string. Callers are advised not rely on the result being a new string nor on it being eq to an existing string.

In particular, mutating the returned value may inadvertently change another string, alter a constant string in the program, or even raise an error. To obtain a string that you can safely mutate, use copy-sequence on the result.

For information about other concatenation functions, see the description of mapconcat in Mapping Functions, vconcat in Vector Functions, and append in Building Lists. For concatenating individual command-line arguments into a string to be used as a shell command, see combine-and-quote-strings.

function split-string string \&optional separators omit-nulls trim​

This function splits string into substrings based on the regular expression separators (see Regular Expressions). Each match for separators defines a splitting point; the substrings between splitting points are made into a list, which is returned.

If separators is nil (or omitted), the default is the value of split-string-default-separators and the function behaves as if omit-nulls were t.

If omit-nulls is nil (or omitted), the result contains null strings whenever there are two consecutive matches for separators, or a match is adjacent to the beginning or end of string. If omit-nulls is t, these null strings are omitted from the result.

If the optional argument trim is non-nil, it should be a regular expression to match text to trim from the beginning and end of each substring. If trimming makes the substring empty, it is treated as null.

If you need to split a string into a list of individual command-line arguments suitable for call-process or start-process, see split-string-and-unquote.

Examples:

(split-string "  two words ")
β‡’ ("two" "words")

The result is not ("" "two" "words" ""), which would rarely be useful. If you need such a result, use an explicit value for separators:

(split-string "  two words "
split-string-default-separators)
β‡’ ("" "two" "words" "")
(split-string "Soup is good food" "o")
β‡’ ("S" "up is g" "" "d f" "" "d")
(split-string "Soup is good food" "o" t)
β‡’ ("S" "up is g" "d f" "d")
(split-string "Soup is good food" "o+")
β‡’ ("S" "up is g" "d f" "d")

Empty matches do count, except that split-string will not look for a final empty match when it already reached the end of the string using a non-empty match or when string is empty:

(split-string "aooob" "o*")
β‡’ ("" "a" "" "b" "")
(split-string "ooaboo" "o*")
β‡’ ("" "" "a" "b" "")
(split-string "" "")
β‡’ ("")

However, when separators can match the empty string, omit-nulls is usually t, so that the subtleties in the three previous examples are rarely relevant:

(split-string "Soup is good food" "o*" t)
β‡’ ("S" "u" "p" " " "i" "s" " " "g" "d" " " "f" "d")
(split-string "Nice doggy!" "" t)
β‡’ ("N" "i" "c" "e" " " "d" "o" "g" "g" "y" "!")
(split-string "" "" t)
β‡’ nil

Somewhat odd, but predictable, behavior can occur for certain β€œnon-greedy" values of separators that can prefer empty matches over non-empty matches. Again, such values rarely occur in practice:

(split-string "ooo" "o*" t)
β‡’ nil
(split-string "ooo" "\\|o+" t)
β‡’ ("o" "o" "o")

variable split-string-default-separators​

The default value of separators for split-string. Its usual value is "[Β \f\t\n\r\v]+".