Printf function similar to C language

zhaozj2021-02-16  45

The function fmt helps you to format a string in vbscript as you do in C.

IN C, if you write:

Printf ("THIS% S Number% D", "Test", 1);

THEN You Would Use The Function FMT in Vbscript Like this:

DIM strstr = fmt ("this is% x number% x", Array ("Test", 1))

Detailsthe 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' usage example 'type.': 'Dim str 'Str = FMT ("Hello, Mr.% X, Today's Date IS% X.", Array ("Miller", Date)' response.write strfunction 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

'Expand from Array. Elseif MID (STR, I 1, 1) = "x" Then Res = Res & CSTR (args (pos)) POS = POS 1 i = i 1 end if endiff

'Found a Normal Char. Else Res = Res & Mid (STR, I, 1) End if Next

FMT = Resend FunctionThe Format Character Is Always% X, Independent of The Actual Type, Since Vbscript Has No Direct Types Like Integer or String.

转载请注明原文地址:https://www.9cbs.com/read-24794.html

New Post(0)