其他分享
首页 > 其他分享> > [ Skill ] print println printf fprintf sprintf lsprintf

[ Skill ] print println printf fprintf sprintf lsprintf

作者:互联网

https://www.cnblogs.com/yeungchie/

几种 print 函数的差异

print

print( 12345 )        ; 12345
print( "YEUNGCHIE" )  ; "YEUNGCHIE"
print( winId )        ; window:1
print( cvId )         ; db:0x21e4c01a
print( "String" port )

println

print( 1 ) print( 2 ) print( 3 )
; 123

同时运行 3 个 println

println( 1 ) println( 2 ) println( 3 )
; 1
; 2
; 3

printf

who = "YEUNGCHIE"
printf( "My name is %s and weight is %d kg\n" who 999 )
; My name is YEUNGCHIE and weight is 999 kg

更详细的使用方法可以看以前的这篇随笔 [ Skill ] 中的通用输出格式规范

fprintf

file = outfile( "~/text.txt" )
fprintf( file "My name is %s and weight is %d kg\n" "YEUNGCHIE" 999 )
close(file)
> cat ~/text.txt
My name is YEUNGCHIE and weight is 999 kg

sprintf

sprintf( variable "My name is %s and weight is %d kg\n" "YEUNGCHIE" 999 )
variable = sprintf( nil "My name is %s and weight is %d kg\n" "YEUNGCHIE" 999 )
; "My name is YEUNGCHIE and weight is 999 kg"

上面两句的效果是一样的,variable 将会被赋值为格式化后的字符串。

lsprintf

string = lsprintf( "My name is %s and weight is %d kg\n" "YEUNGCHIE" 999 )
format = "My name is %s and weight is %d kg\n"
args   = list( "YEUNGCHIE" 999 )
string = apply( 'lsprintf format args )
; "My name is YEUNGCHIE and weight is 999 kg"
string = apply(
  lambda(( a \@rest b )
    apply( 'sprintf nil a b )
  )
  format
  args
)
; "My name is YEUNGCHIE and weight is 999 kg"

标签:name,lsprintf,999,YEUNGCHIE,weight,print,fprintf,println,My
来源: https://www.cnblogs.com/yeungchie/p/15758985.html