Questions tagged [string]

A string is a sequence of characters, which can be user data or used internally in Emacs. For example, string to search, replace, save in a register and so on, file name, etc.

Manipulating strings of characters in Emacs Lisp.

Useful links:

153 questions
38
votes
2 answers

How to check in elisp if a string is a substring of another string?

How to check if a string s1 is a substring of another string s2? For example (test-substring "f t" "df tj") --> t, (test-substring "ft" "df tj") --> nil.
Name
  • 7,689
  • 4
  • 38
  • 84
28
votes
1 answer

How to strip decorations (text properties) from a string?

By a "decorated string" I mean something like #("foo" 0 4 (fontified t face font-lock-function-name-face)) ...as opposed to the "plain string" "foo" (If Elisp code reads a string directly from a buffer, it may be decorated like this.) What's the…
kjo
  • 3,145
  • 14
  • 42
22
votes
1 answer

Get content of a buffer

So buffer-string gets the content of the current buffer. But it doesn't allow specifying other buffers. How can I get around that? Do I need something like save-window-excursion to make it work?
Maciej Goszczycki
  • 1,777
  • 13
  • 18
17
votes
2 answers

Heredoc or equivalent multiline string syntax in Elisp?

In common-lisp we have the library cl-heredoc, is there an equivalent in EmacsLisp?
ocodo
  • 1,202
  • 11
  • 20
12
votes
3 answers

How to wrap a single string literal across multiple lines?

Is it possible to write a single line for an Elisp string: "hello world" As: "hello world" Without inserting a newline character? (wrapping a string literal). I read that adding a tilde at the end of the line does this in some lisp's, but its not…
ideasman42
  • 8,375
  • 1
  • 28
  • 105
12
votes
2 answers

How do i get rid of "default" (last used) string in some emacs functions?

I have the following problem : when i use 'C-x r t' (string-rectangle), i am prompted to enter some string, to replace the selected rectangle. Lets say i type ";;" (to comment out the rectangle). This works just fine, but the next time i call the…
mation
  • 121
  • 3
10
votes
3 answers

What's the idiomatic (or best) way to trim surrounding whitespace from a string?

I'm working with strings which may have any number of prefix and suffix spaces, tabs, newlines, etc. Currently I have this: (replace-regexp-in-string "^[^[:alnum:]]*\\(.*\\)[^[:alnum:]]*$" "\\1" my-string)
user23847
  • 101
  • 1
  • 3
9
votes
2 answers

Interpolate nil argument to `format` as the empty string?

I understand that this is trivial with an if, but is there an option, like %S or %s that interpolates nil as no string at all? Example: (format "%?.el" nil) ; ".el" (format "%?.el" "beginner") ; "beginner.el"
The Unfun Cat
  • 2,393
  • 16
  • 32
9
votes
2 answers

How can I turn a string into its literal representation?

(mystery-function "a neat\nstring\"") => "\"a neat \\nstring\\\"" This question was asked on freenode#emacs by user dropdrive. How could I write (or use) a function that takes a string literal and returns the string literal that, when read, would…
Sean Allred
  • 6,861
  • 16
  • 85
9
votes
4 answers

Which keyboard shortcut to use for navigating out of a string

When I'm in the middle of a looooong string, like the following (setq Emacs-beta "Which keyboard shortcut to use for navigating out of a string") Can I skip out of it, just before the first " (after Emacs-beta) with a shortcut?
Dieter.Wilhelm
  • 1,836
  • 14
  • 25
8
votes
3 answers

Split a complicated string?

Q: how do I split a complicated string when whitespace delimiters aren't discriminating enough? Background I'm working with BibTeX files. I want to split an author string of the form "first-name middle-name last-name" into its tokens. Doing so is…
Dan
  • 32,584
  • 6
  • 98
  • 168
7
votes
2 answers

Split a string without consuming separators?

I frequently need to split strings, while keeping the separator. Researching the elisp manual, I am not finding a way to split a string according to a separator, without consuming the separator itself. For example, split-string splits a given string…
gsl
  • 1,742
  • 17
  • 34
6
votes
1 answer

How can I convert a string form of a list to an actual list?

I would like to convert these two strings "(a b c)" "(9 . 3)" to these (a b c) (9 . 3) I'd had some luck with the first one evaluating this, (mapcar 'intern (split-string (string-trim "(a b c)" "(" ")"))) basically, removes parentheses from string,…
C11g
  • 333
  • 1
  • 7
6
votes
1 answer

Single function to return file contents as a string

I'm wondering if there's a terse (i.e. single function) approach for reading a file's content given its path and returning it as a string. My go-to has been: (with-temp-buffer (insert-file-contents some-path) (buffer-string)) => "it's a short…
ebpa
  • 7,319
  • 26
  • 53
6
votes
4 answers

How to check if a given string is a substring of an element of a list

According to documentation, member checks if a a given element is an element of a list. For example if the list, say my_list consists of apple and orange, (member "apple" my_list) returns true. Is there an easy way to check if a given string is a…
Name
  • 7,689
  • 4
  • 38
  • 84
1
2 3
10 11