Elisp Strings
Table of Contents
1. Useful Functions Related to Strings
1.1. Length Query
Returns the length of string.
(length SEQUENCE)
;; E.G.
(length "abc") ; => 3
1.2. Get Substrings
(substring STRING &optional FROM TO)
;; e.g.
(substring "abs123" 0 3) ; => "abs"
Return a substring from position FROM to TO. 0-indexed string. By default, TO is to the end of string, and FROM is 0.
If negative, count from right.
1.3. String Concatenation
First, introduce concat function. It accepts a list of arguments that each argument can be a string, a list or a vector of characters.
(concat &rest SEQUENCES)
Next, introduce mapconcat function. It applies FUNCTION to each argument of SEQUENCE, then concatenate results together separated by SEPARATOR.
(mapconcat FUNCTION SEQUENCE &optional SEPARATOR)
Thirdly, vconcat function takes multiple vectors or lists and flattens them into a single vector. It can also convert a string into a vector of characters (integers)
(vconcat &rest SEQUENCES)
;; e.g.
(vconcat [a b c] [d e f]) ; => [a b c d e f]
(vconcat '(1 2 3) [4 5 6]) ; => [1 2 3 4 5 6]
(vconcat "hello") ; => [104 101 108 108 111]
Similarly, there’s a function called append which does similar job, but makes the result a list. All arguments except the last argument are copied.
(append &rest SEQUENCES)
;; e.g.
(append '(1 2) '(3 4) '(5 6)) ; => (1 2 3 4 5 6)
1.4. Split String
(split-string STRING &optional SEPARATOR OMIT-NULLS TRIM)
;; e.g.
(split-string "x_y_z" "_") ; => ("x" "y" "z")
It splits a string with separator and returns a list.