Elisp: String Functions
Here is a complete list of string functions.
Length, Substring
length
-
(length SEQUENCE)
Return the length of a Sequence.
(length "abc") ;; 3
substring
-
(substring STRING &optional FROM TO)
return a substring from position FROM to TO. Position start at 0. By default, TO is to end of string, and FROM is 0. If negative, count from right.
(substring "abc123" 0 3) ;; "abc"
Join Strings
use one of:
concat
→ join.format
→ join by embedding into arbitrary text. Elisp: Format Stringmapconcat
→ join with separator. Elisp: Sequence Join, Convert
concat
-
(concat &rest SEQUENCES)
join all the arguments and make the result a string.
(concat "some" "thing")
Split String
split-string
-
(split-string STRING &optional SEPARATORS OMIT-NULLS TRIM)
Split STRING into substrings bounded by matches for SEPARATORS.
;; split string into parts, returns a list (split-string "xy_007_cat" "_")
Convert String and Number
string-to-number
number-to-string
format
Buffer Text to String
String to Buffer
Match String by Regex
string-match
-
(string-match REGEXP STRING &optional START)
Return the index of beginning of first match in STRING. Return nil if no match.
(let ((case-fold-search t)) (string-match "3" "xx3x")) ;; 2
Get Regex Captured String
match-string
Regex Replace in String
replace-regexp-in-string
-
(replace-regexp-in-string REGEXP REP STRING &optional FIXEDCASE LITERAL SUBEXP START)
Replace string by regex.(replace-regexp-in-string "</*div>" "<p>" "<div>something</div>") ;; return ;; "<p>something<p>"
Trim String
string-trim-left
string-trim-right
string-trim
These are new in
Emacs 24.4 (Released 2014-10)
.
Before
Emacs 28 (Released 2022-04)
,
you need to first load
(require 'subr-x)
.
〔see Emacs Version History〕
String Comparison, Equality Test
Convert String
vconcat
→ string to vectorappend
→ string to listbyte-to-string
Character and String
Other String Functions
To use the following, you may need to first load the lib:
(require 'subr-x)
string-blank-p
→ is empty or whitespace.string-empty-p
→ is empty.string-join
→ join with separators.string-reverse
(obsolete. usereverse
)string-remove-prefix
string-remove-suffix
string-pad
Misc String Functions
stringp
→ is stringstring-or-null-p
make-string
substring-no-properties
- split-string-default-separators → variable
store-substring
→ modify stringclear-string
assoc-string
Letter Case
downcase
upcase
capitalize
upcase-initials
Case Table
case-table-p
set-standard-case-table
standard-case-table
current-case-table
set-case-table
set-case-syntax-pair
set-case-syntax-delims
set-case-syntax
with-case-table
- ascii-case-table
describe-buffer-case-table