25
votes
Accepted
How to strip decorations (text properties) from a string?
[Your propertized string looks wrong - perhaps you copied it wrong. foo has only 3 characters, so it is impossible for it to be fontified on chars 0 to 4 (i.e., chars 0, 1, 2, and 3 - that's 4 chars)....
17
votes
Accepted
How to wrap a single string literal across multiple lines?
Yes, you can use a literal string that does not include the newline, so that the newline shows only for your code.
Use \ to escape the newline, so that it is not included in the string:
(setq foo "...
17
votes
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
The built-in library subr-x.el has included the inline functions string-trim-left, string-trim-right, and string-trim ...
9
votes
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
There is the string manipulation library s.el where trimming whitespace and newlines at the beginning and the end of a string is implemented as function s-trim. I cite that function here with its ...
9
votes
Accepted
Filter a list of strings to create a separate list of those that match a given prefix
(seq-filter (apply-partially #'string-prefix-p "a")
'("apple" "pear" "grape" "apricot"))
If you have other similar questions, consult the ...
8
votes
How to wrap a single string literal across multiple lines?
Edit, it seems multi-line literals are supported, keeping answer since it may be useful still.
You can however use concat. eg:
(concat "hello"
"world")
8
votes
Split line every n characters
For occasional use, you can do with query-replace-regexp (C-M-%).
Replace .\{32\} with \&^J. This means, replace all chunks of 32 characters, with the same thing followed by a newline. To type ...
8
votes
Accepted
Regex for the last line of a string
$ matches at the end of a line, not the end of a string. If you want to match at the end of a string you need to use the \' operator:
(string-match "\n.*\\'" "\n \n \n ")
=> 4
...
7
votes
Accepted
Single function to return file contents as a string
There is not. You can report-emacs-bug and suggest that one be included.
One reason I can think of for not including such a function is that there are different reasonable ways to handle errors. E.g. ...
7
votes
Accepted
How to programmatically surround a string with escaped double-quote
You might be thinking of prin1-to-string:
(prin1-to-string "foo")
=> "\"foo\""
You probably need some protection though (ensure it's a string, strip text properties, ...
7
votes
How to check if a given string is a substring of an element of a list
The easiest way is to use the Common Lisp compatibility layer:
(require 'cl-seq)
(cl-member "ap" '("foo" "apa" "bar") :test #'string-match)
==> ("apa" "bar")
PS. This is not directly relevant to ...
7
votes
Accepted
Difference between (quote string) and "string"
'list and "list" are different datatypes:
ELISP> (type-of 'list)
symbol
ELISP> (type-of "list")
string
Note that they even show up differently in your example -- it's the difference between ...
6
votes
Accepted
Split a complicated string?
Behold:
(defun fancy-split (input)
(let (tokens)
(with-temp-buffer
(insert input)
(goto-char (point-min))
(while (not (eobp))
(cond
((looking-at "{")
...
6
votes
Accepted
Set difference for sets of strings
It is for the same reason that:
(eql "foo" "foo")
=> nil
Along with the other cl-lib sequence functions generally, set-difference (aka cl-set-difference) defaults to using eql for its equality ...
6
votes
Accepted
How can I convert a string form of a list to an actual list?
ELISP> (read "(a b c)")
(a b c)
ELISP> (read "(9 . 3)")
(9 . 3)
If by (9 . 3) you mean you'd like a cons, then my answer would work. Note however if you actually would like ...
5
votes
How to check if a given string is a substring of an element of a list
The function that comes to my mind at least would be cl-member using a string-match-p test.
Here's a short example that shows how to use it:
ELISP> (setq testing '("an apple" "a pear" "a grape"))
...
5
votes
Accepted
Return nth line from (current) buffer
This could be done with the next function (with or without narrowing):
(defun get-nth-line (number)
"Get line with NUMBER"
(save-restriction
(widen)
(save-excursion
(goto-line ...
5
votes
Accepted
How to concatenate a string and a list?
AFAIK the canonical way to turn a list into a string is with mapconcat, e.g.
(defun concat-string-list (str xs)
(concat str " "
(mapconcat #'symbol-name xs " ")))
5
votes
Accepted
Getting characters from current position to end of line as a substring
I don't believe there is a standard elisp function to do exactly what you ask, but it isn't hard to compose your own. Here is my stab at what you describe:
(defun gw-string-to-end-of-line ()
"...
5
votes
How to convert string to ascii or unicode hex?
(defun hexify-string (strg)
(mapconcat (lambda (c) (format "%x" c)) strg ""))
5
votes
Accepted
Emacs 29 docstring single quote escaping rules, compiler level event?
Documentation is:
C-hig (elisp)Keys in Documentation
C-hig (elisp)Text Quoting Style
In short, the text-quoting-style feature introduced in Emacs 25 made
it impossible to use certain characters in ...
4
votes
Accepted
What is the best way to search forward/backward for a token?
You used tag isearch so presumably you want to search interactively, not using Lisp. And you used tag words; you apparently want to search for a whole word.
In that case, just use M-s w, which does ...
4
votes
Accepted
Interpolate environment variables in string
I think you're looking for the function substitute-env-vars:
substitute-env-vars is a compiled Lisp function in ‘env.el’.
(substitute-env-vars STRING &optional WHEN-UNDEFINED)
Substitute ...
4
votes
Accepted
match-data fails to consider only last search with string-match and persists across sessions
The match object you're accessing is global and may be changed by any piece of lisp code. Therefore you should immediately check its state. The following piece of code shows how and exhibits the ...
4
votes
Sort lines in a string
Here is one possible way to implement this using a temporary buffer:
(defun sort-string (string &optional reverse)
"sorts an input string, using a temporary buffer"
;;(interactive "sto be ...
4
votes
Accepted
Non-hex digit used for Unicode escape error when setting org-latex-classes
I just found out what's wrong. Elisp requires a backslash in a string (i.e., between double-quotes "") to be escaped in Lisp, so the following code works:
(setq org-latex-classes
'(("article"
...
4
votes
Accepted
Different backslash behaviour between interactive regexps and code regexps
Elisp regexps are represented as strings, which means backslashes are interesting, as they are not only special to regexps, but also when writing strings.
Emacs requires a literal \ character to be ...
4
votes
Colored string in mode line
@NickD provided a good answer: use a face.
OP's comment to Nick's answer says that he'll try to write a function that, given a string, returns a propertized string. Such functions already exist: ...
4
votes
Accepted
org-babel-tangle error
A good tool for finding errors in Org mode files is org-lint: just visit the file, say M-x org-lint and fix any problems it finds. There is no guarantee that it will find everything, so you might ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
string × 156regular-expressions × 20
replace × 12
list × 10
text-properties × 10
elisp × 8
org-mode × 7
buffers × 7
characters × 7
symbols × 6
syntax-highlighting × 5
interactive × 5
numbers × 5
search × 4
variables × 4
equality × 4
org-agenda × 3
faces × 3
files × 3
major-mode × 3
region × 3
display × 3
syntax × 3
insert × 3
print × 3