Emacs 菜鸡教程

Elisp String

Creating

make-string

Function: make-string count character
ELISP> (make-string 5 ?x)
"xxxxx"

substring

substring string start &optional end
ELISP> (substring "abcdefg" 0 3)
"abc"
ELISP> (substring "abcdefg" -3 -1)
"ef"
ELISP> (substring "abcdefg" -3 )
"efg"
ELISP> (substring "abcdefg" -3 nil)
"efg"
ELISP> (substring "abcdefg" 0)
"abcdefg"

concat

ELISP> (concat "The " "quick brown " "fox.")
"The quick brown fox."
ELISP> 

ELISP> (mapconcat 'identity '("aaa" "bbb" "ccc") ",")
"aaa,bbb,ccc"
ELISP> (split-string "aaa,bbb,ccc" ",") 
ELISP> (split-string "aaa,bbb,ccc" ",")
("aaa" "bbb" "ccc")

split

 Function: split-string string &optional separators omit-nulls trim 
ELISP> (split-string "  two words ")
("two" "words")

ELISP> (split-string "hello_world" "_" t)
("hello" "world")

Join

 (apply #'concat '("hello, " "world"))
    ==> "hello, world"

Comparison

(string= "abc" "ABC")
     ⇒ nil
(string< "abc" "abd")
     ⇒ t

Formatting

(format "The name of this buffer is %s." (buffer-name))
     ⇒ "The name of this buffer is strings.texi."

Case Convertion

downcase, upcase

(downcase "The cat in the hat")
     ⇒ "the cat in the hat"
(upcase "The cat in the hat")
     ⇒ "THE CAT IN THE HAT"

capitalize

(capitalize "THE 77TH-HATTED CAT")
     ⇒ "The 77th-Hatted Cat"

Exercise

case 1

把c风格的变量trans_amount改为Camal风格的变量TransAmount

(defun bohao-camal-variable ()
  (interactive)  
  (let ((str (thing-at-point 'symbol)))
    (insert " " (apply 'concat
                       (mapcar 'capitalize (split-string str "_"))))))

(thing-at-point thing)

---------- Buffer: foo ----------
Gentlemen may cry ``Pea∗ce! Peace!,''
but there is no peace.
---------- Buffer: foo ----------

(thing-at-point 'word)
     ⇒ "Peace"
(thing-at-point 'line)
     ⇒ "Gentlemen may cry ``Peace! Peace!,''\n"
(thing-at-point 'whitespace)
     ⇒ nil