使VB实现类似于C语言的printf函数的效果

  在C语言中有个产生格式化输出的函数Printf,假如b的值为24,则使用函数printf(“the value of printf is:%d”,a)输出为the value of printf is:24。

  下面这个函数就能使你的VB能实现Printf的效果,用于批量格式化的时候很给力的。

‘——————————————————–
‘Name: vPrintf
‘Argument: uStr
‘Return:
‘Description: 该函数能实现类似于C语言的printf函数的效果
‘usage example:
‘   dim str
‘   str = vPrintf( “Hello, Mr. %x, today’s date is %x.”, Array(“Miller”,Date) )
‘   response.Write str
‘——————————————————–

Function vPrintf(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)
     If Mid(str, i, 1) = “%” Then
       If i < Len(str) Then
           If Mid(str, i + 1, 1) = “%” Then
             res = res & “%”
             i = i + 1

           ElseIf Mid(str, i + 1, 1) = “x” Then
             res = res & CStr(args(pos))
             pos = pos + 1
             i = i + 1
           End If
       End If
     Else
       res = res & Mid(str, i, 1)
     End If
   Next
   vPrintf = res
End Function

  下载链接

  

您可能还喜欢...

1 条回复

  1. 王3峰说道:

    主题设置的很不错啊。

回复 王3峰 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据