类似于C语言的printf函数 (转)

类似于C语言的printf函数 (转)[@more@]

The function fmt helps you to format a string in vbscript as you do in C.

In C, if you write:

printf( "this is %s number %d", "test", 1 );

then you would use the function fmt in VBScript like this:

dim str
str = fmt( "this is %x number %x", Array("test", 1) )

Details
The complete function looks like this:

' works like the printf-function in C.
' takes a string with format characters and an array
' to expand.
'
' the format characters are always "%x", independ of the
' type.
'
' usage example:
'  dim str
'  str = fmt( "hello, Mr. %x, today's date is %x.", Array("Miller",Date) )
'  response.Write str
function fmt( str, args )
  dim res  ' the result string.
  res = ""

  dim pos  ' the current position in the args array.
  pos = 0

  dim i
  for i = 1 to Len(str)
  ' found a fmt char.
  if Mid(str,i,1)="%" then
  if i  ' normal percent.
  if Mid(str,i+1,1)="%" then
  res = res & "%"
  i = i + 1

  ' expand from array.
  elseif Mid(str,i+1,1)="x" then
  res = res & CStr(args(pos))
  pos = pos+1
  i = i + 1
  end if
  end if

  ' found a normal char.
  else
  res = res & Mid(str,i,1)
  end if
  next

  fmt = res
end function
The format character is always %x, independent of the actual type, since VBScript has no direct types like integer or string.


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10748419/viewspace-987390/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10748419/viewspace-987390/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值