(string= "abc" "abc") ;=> t
(string= "abc" "ABC") ;=> nil
(string= "AB" "ABC") ;=> nil
(compare-strings "ABC" nil nil "abc" nil nil t) ;=> t
(length "abcdefg") ;=> 7
(length "日本語") ;=> 3
; 普通に結合
(concat "abc" "-def") ;=> "abc-def"
(concat "私は" "美味しく食べました") ;=> "私は美味しく食べました"
; nilは無視
(concat "abc" nil "-def")
; 引数なしの場合は空文字列を返す
(concat) ;=> ""
(mapconcat #'identity '("Hello" "Emacs" "Lisp") "-") ;=> "Hello-Emacs-Lisp"
(make-string 10 ?X) ;=> "XXXXXXXXXX"
(apply #'concat (make-list 5 "Hello")) ;=> "HelloHelloHelloHelloHello"
(mapconcat #'identity (make-vector 5 "Hello") ",") ;=> "Hello,Hello,Hello,Hello,Hello"
(upcase "The cat in the hat") ;=> "THE CAT IN THE HAT"
(downcase "THE CAT IN THE HAT") ;=> "the cat in the hat"
;; 一文字目を大文字に、残りは全て小文字にする
(capitalize "The cat in the hat") ;=> "The Cat In The Hat"
(capitalize "THE 77TH-HATTED CAT") ;=> "The 77th-Hatted Cat"
;; 一文字目を大文字に、残りは何もしない
(upcase-initials "The cat in the hat") ;=> "The Cat In The Hat"
(upcase-initials "THE 77TH-HATTED CAT") ;=> "THE 77TH-HATTED CAT"
(require 'cl)
(let ((case-fold-search nil))
(map 'string
#'(lambda (c)
(if (char-equal (upcase c) c)
(downcase c)
(upcase c)))
"i lOVE eMACS."))
;=> "I Love Emacs."
(shell-command-to-string "file -ib /bin/bash") ;=> "application/x-executable\n"
; 0(a)から、3(d)なので"abc"を生成
(substring "abcdefg" 0 3) ;=> "abc"
; 負の添え字は逆から数える
(substring "abcdefg" -3 -1) ;=> "ef"
; 第2引数は省略可能で、終端までコピーを表す
(substring "abcdefg" 2) ;=> "cdefg"
; 先頭に0を渡すと文字列のコピーと等価になる
(substring "abcdefg" 0) ;=> "abcdefg"
(require 'cl)
(setq s "Apple Banana Orange")
(setf (substring s 0 5) "Vine") ;=> "Vine"
s ;=> "Vine Banana Orange"
(split-string "Soup is good food" "o") ;=> ("S" "up is g" "" "d f" "" "d")
(split-string "Soup is good food" "o+") ;=> ("S" "up is g" "d f" "d")
(split-string "Soup is good food") ;=> ("Soup" "is" "good" "food")
(substitute-in-file-name "LANG=${LANG}") ;=> "LANG=ja_JP.UTF-8"
(require 'cl)
(setq str "a b c d\n** ** ** *\n123\n456\n")
(map nil '(lambda (x) (message x)) (split-string str ""))
;=> *Messages*
;a
;
;b
;
;c
;
;d
; [2 times]
;* [7 times]
; [2 times]
;1
;2
;3
; [2 times]
;4
;5
;6
; [2 times]
(setq str "a b c d\n** ** ** *\n123\n456\n")
(map nil '(lambda (x) (message x)) (split-string str "\n"))
;=> *Messages*
;a b c d
;** ** ** *
;123
;456
(car (split-string (car (cdr (split-string " abcd " "^ +"))) " +$")) ;=> "abcd"
; 正規表現を使ってみる
(replace-regexp-in-string "^\\s-+\\|\\s-+$" "" " \t abc def \t ") ;=> "abc def"
(string-to-number "256") ;=> 256
(string-to-number "256.0") ;=> 256.0
(string-to-number "-256.5") ;=> -256.5
;; 基数を指定する
(string-to-number "110") ;=> 110
(string-to-number "110" 8) ;=> 72 (#o110)
(string-to-number "110" 16) ;=> 272 (#x110)
(number-to-string 256) ;=> "256"
(number-to-string -256.0) ;=> "-256.0"
(number-to-string -256.5) ;=> "-256.5"
(string-to-number "10.1") ;=> 10.1
;; 左詰め
(format "%10s" "foo") ;=> " foo"
(format "%10d" 1234) ;=> " 1234"
;; 右詰め
(format "%-10s" "foo") ;=> "foo "
(format "%-10d" 1234) ;=> "1234 "
(setq s "Apple Banana Apple Orange")
(replace-regexp-in-string "Apple" "Pine" s) ;=> "Pine Banana Pine Orange"
(setq s "Apple Banana Apple Orange")
(string-match "Apple" s) ;=> 0
(string-match "Banana" s) ;=> 6
replace-regexp-in-string "\n+$" "" STRING)
(split-string "001,TAKEUCHI Hitoshi,Yokohama" ",") ;=> ("001" "TAKEUCHI Hitoshi" "Yokohama")
; エンコード
(encode-coding-string "漢字です" 'sjis)
(encode-coding-string "漢字です" 'euc-jp)
(encode-coding-string "漢字です" 'utf-8)
(encode-coding-string "漢字です" 'emacs-mule)
; デコード
(decode-coding-string (encode-coding-string "漢字です" 'utf-8) 'utf-8)
;=> "漢字です"
(format "このバッファは %s." (buffer-name)) ;=> "このバッファは *scratch*."
;整数 %d
(format "%d" 100) ;=> "100"
;浮動小数点その1 %f
(format "%f" 100.0) ;=> "100.000000"
;浮動小数点その2 %g できるだけ短くなる出力にする
(format "%g" 100.00) ;=> "100"
(format "%g" 100.05) ;=> "100.05"
(format "%g" 1000000000000000) ;=> "1e+015"
(format "%g" 0.00000000000000000001) ;=> "1e-020"
;ダブルクォートなどをエスケープして出力
(format "この文字列 %S のダブルクォートなどはエスケープされる" "test")
;=> "この文字列 \"test\" のダブルクォートなどはエスケープされる"
(format "この文字列 %S のダブルクォートなどはエスケープされる" "\"test\"")
;=> "この文字列 \"\\\"test\\\"\" のダブルクォートなどはエスケープされる"