类似于C语言的printf函数
2008-05-01 05:04
225 查看
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.
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.
相关文章推荐
- 类似于C语言的printf函数
- C语言——printf函数的结合顺序
- C语言printf函数详解
- C语言printf()函数:格式
- C语言 Printf函数
- c语言:请编程序将“China”译成密码,分别用putchar和printf函数输出这5个字符。
- C语言:自定义一个查找字串的功能函数,类似于<string.h>中的strstr()
- C语言中最常用的三种输入输出函数scanf()、printf()、getchar()和putchar()
- C语言stdio.h中printf函数输出‘12’结果理解
- C语言printf()函数中一些不为人知的技巧!
- 第02天C语言(12):printf-函数
- 【C语言】关于printf函数输出字符格式
- C语言中printf格式化输出函数
- c语言可变参数原理以及printf函数的自实现
- C语言中printf格式化输出函数
- c语言不定参数与printf函数的实现
- 【C语言】printf()函数详解
- Java中实现类似于C语言的中函数指针的功能
- 对c语言中printf函数的个人理解
- c语言中可变参数的原理---printf()函数