|
Comments and Discussions
|
 |
 |
Hi Guys,
If you are interested in using python like string interpolation in VBScript / JScript then I have some code which will make the following work...
greeting = "Hello"
firstName = "SDX"
lastName = "2000"
dim i
i=1
print "$greeting $firstName ${LastName}!"
print "3+5=${3+5}"
print "Square root of 81 is ${sqr(81)}"
print "${i = i+1}" print "${msgbox(firstName & lastName,0,greeting)}"
Output
Hello SDX 2000!
3+5=8
Square root of 81 is 9
False
1
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo
|
|
|
|
 |
I added the functionality to use fixed width field lengths. This is useful when writing output that needs neat aligned columns.
'============================================================================
' takes a string with format characters and an array
' to expand.
'
' the format characters are always "%x", independ of the
' type.
'
' Use double percent (%%) to denote an actual percent
'
' usage example:
' dim str
' str = fmt( "hello, Mr. %x, today's date is %x.", Array("Miller",Date) )
' response.Write str
'
' additionally the support was added to allow for fixed field widths
' adding a number between the % and x will set the field width to at least that size
' example:
' str = fmt( "%10x %10x %10x", Array("Jason", "Steve", "Smith") )
' response.write str
' output:
' 'Jason Steve Smith '
'============================================================================
function fmt( str, args )
dim res ' the result string.
res = ""
Dim oRE 'the RegEx
Set oRE = New regexp
Dim fieldLen ' length of the field
Dim strOffset
Dim argLen ' length of array string
Dim strSearchOn ' string to perform RegEx on
strSearchOn = ""
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
'make sure we are not at the end of the string
if i
|
|
|
|
 |
Yeah this is a great idea there really is a need for fixed length formatting. Good work.
|
|
|
|
 |
For non-ASP stuff:
Const cArg = "%s"
 
Function FormatStr(ByVal S, ByVal Args)
Dim I
FormatStr = S
For I = LBound(Args) To UBound(Args)
If InStr(FormatStr, cArg) <> 0 Then
FormatStr = Replace(FormatStr, cArg, Args(I), 1, 1)
End If
Next
End Function
Michael R. Cessna
Principal Software Engineer
Best Software, Inc (CRM)
|
|
|
|
 |
i don't really love the idea of going by each character, especially when there's a function named split.
the code is split by %s keyword and later reassembled with
the array.
function sprintf(str, args)
dim asplit,pos,cnt
asplit=split(str,"%s")
for i=0 to ubound(asplit)
if i=ubound(asplit) then
sprintf = sprintf & asplit(i)
pos=pos+1
elseif right(asplit(i),1)="%" then
sprintf=sprintf & asplit(i) & "s"
else
sprintf=sprintf & asplit(i) & args(pos)
pos=pos+1
end if
next
end function
Comments?
|
|
|
|
 |
Hi, I just looked at your fmt function; it's exactly what I needed. I added a bWriteResponse flag to cause the function to perform immediate response.write's. Gave it a quick test and it seems to work. hre is the modified code, if you are interested: <% ' ########################################################################### ' # ' # file: ' # fmt.inc ' # ' # description: ' # fmt function. ' # ' # author: ' # Uwe Keim, [email protected] ' # zeta software GbR, Germany. ' # ' # updated: ' # By Wes Rogers 5/13/2002, to have a flag that does a response.write ' # instead of building up a response string to return. If set true, ' # the function just returns an empty string. ' # ' # version: ' # 2000-01-02 Uwe Keim file created. ' # ' ########################################################################### %>
<% ' /////////////////////////////////////////////////////////////////////////// ' // fmt function. ' Works like the printf-function in C. ' takes a string with format characters and an array ' to expand. ' ' The format characters are always "%x", independent of the type. ' ' Usage example: ' dim str ' str = fmt("Hello, Mr. %x, today's date is %x.<br>", _ ' Array("Miller",Date), _ ' true) ' ' Note: To output a normal percent sign, use two in a row: %%. ' /////////////////////////////////////////////////////////////////////////// function fmt( str, args, bWriteResponse ) 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 if bWriteResponse then response.Write "%" else res = res & "%" end if i = i + 1 ' expand from array. elseif Mid(str,i+1,1)="x" then if bWriteResponse then response.Write CStr(args(pos)) else res = res & CStr(args(pos)) end if pos = pos+1 i = i + 1 end if end if ' found a normal char. else if bWriteResponse then response.Write Mid(str,i,1) else res = res & Mid(str,i,1) end if end if next fmt = res end function
' /////////////////////////////////////////////////////////////////////////// %>
|
|
|
|
 |
I liked the idea. I miss c libraries too!! But,
string concatenation is generally a very bad idea in
vbscript. Microsoft states that string concats are performed in quadratic time. Would you consider updating
your code to perform Response.Write's. This approach
would be closer to printf, as well
|
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
First Posted | 20 Jan 2000 |
Views | 104,905 |
Downloads | 487 |
Bookmarked | 17 times |
|
|