How to get selected text in Emacs Lisp?

The accepted answer pointed me to the right answer. I want to leave this piece of code for more ELisp beginners like me. regionp contains the "selection" (known as region in ELisp) as a variable in the ELisp function. The if condition checks if the region is active.

(defun get-selected-text (start end)
  (interactive "r")
    (if (use-region-p)
        (let ((regionp (buffer-substring start end)))
            (message regionp))))

Selected text is called region in Emacs universe. See How do I access the contents of the current region in Emacs Lisp?

Tags:

Emacs

Elisp