基本用法和簡單指令

格式的前兩個引數是輸出流和控制字串。基本用法不需要​​額外的引數。當流寫入*standard-output*時傳遞 t

> (format t "Basic Message")
Basic Message
nil

該表示式將 Basic Message 寫入標準輸出,並返回 nil

傳遞 nil 作為流建立一個新字串,並返回它。

> (format nil "Basic Message")
"Basic Message"

大多數控制字串指令需要額外的引數。~a 指令(美學)將列印任何引數,就像 princ 程式一樣。這將列印沒有任何轉義字元的表單(關鍵字列印時沒有前導冒號,字串沒有其周圍的引號,等等)。

> (format nil "A Test: ~a" 42)
"A Test: 42"
> (format nil "Multiples: ~a ~a ~a ~a" 1 (list 2 3) "four five" :six)
"Multiples: 1 (2 3) four five SIX"
> (format nil "A Test: ~a" :test)
"A Test: TEST"
> (format nil "A Test: ~a" "Example")
"A Test: Example"

~a 可選擇右側或左側輸入基於附加輸入。

> (format nil "A Test: ~10a" "Example")
"A Test: Example   "
> (format nil "A Test: ~10@a" "Example")
"A Test:    Example"

~s 指令就像~a,但它列印轉義字元。

> (format nil "A Test: ~s" 42)
"A Test: 42"
> (format nil "Multiples: ~s ~s ~s ~s" 1 (list 2 3) "four five" :six)
"Multiples: 1 (2 3) \"four five\" :SIX"
> (format nil "A Test: ~s" :test)
"A Test: :TEST"
> (format nil "A Test: ~s" "Example")
"A Test: \"Example\""