VB语言

本类阅读TOP10

·Visual Basic 安装程序的制作!!
·VB中使用EXCEL输出
·一个简单的MP3播放器
·VB程序实现WindowsXP效果的界面!!
·VB打造超酷个性化菜单(六)
·透明位图
·平铺与拉伸MDI窗口的背景图 ~!~
·对《VB程序实现WindowsXP效果的界面》一文的补遗
·从Windows资源管理器中拖动文件
·VB打造超酷个性化菜单(一)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
类似于C语言的printf函数

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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.




相关文章

相关软件