Convert Symbol to a String in Elisp

StringElispSymbols

String Problem Overview


How can I convert a symbol type into a string in Emacs lisp?

I wasn't able to find a symbol-to-string function or anything similar.

String Solutions


Solution 1 - String

Try using symbol-name:

(symbol-name 'some-symbol)

Solution 2 - String

Given a symbol name (a string), you can get the symbol's value. Using Trey Jackson's solution:

(setq ASymbol 10)  => 10
(intern "ASymbol")  => 'ASymbol (e.g. the unevaluated symbol with name "ASymbol")
(symbol-value (intern "ASymbol")) => 10

This is useful if you want to get the values of symbols for which you only have names. For instance, you read them from a string.

A few notes: intern returns the value of the symbol with the given name. If no symbol with such a name exists, it creates a symbol with that name. You can use intern-soft to avoid introducing a new symbol if one with the given name does not exists.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCristianView Question on Stackoverflow
Solution 1 - StringTrey JacksonView Answer on Stackoverflow
Solution 2 - StringPablo A Perez-FernandezView Answer on Stackoverflow