ITPub博客

首页 > 应用开发 > IT综合 > 类似于C语言的printf函数 (转)

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

原创 IT综合 作者:gugu99 时间:2007-12-02 14:38:41 0 删除 编辑
类似于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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3122
  • 访问量
    2226167