基本用法和简单指令

格式的前两个参数是输出流和控制字符串。基本用法不需要​​额外的参数。当流写入*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\""