类似于C语言的printf函数

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 jennyvenus 的 blog

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<len(str) then
                ' 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.

本文关键:none
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top