dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: VB6
Expertise: beginner
Sep 15, 2003

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


PrintF - Transforming template strings

' This VB6 function returns a string with a set of parameters replaced with 
' variables, similar to the C function printf().

Public Function PrintF(strMask As String, ParamArray Values()) As String
    On Error Resume Next
    ' pass in a mask with variables in the order of \0, \1, \2, etc
    ' if they are found, replace with the corresponding values()
    Dim lngNbrValues As Long, lngCount As Long
    Dim strX As String, strFind As String
    
    lngNbrValues = UBound(Values)
    
    ' first, find any \\, \t or \n and replace those    
    strX = Replace(strMask, "\\", "¥¥¥")
    strX = Replace(strX, "\t", vbTab)
    strX = Replace(strX, "\n", vbNewLine)
    
    ' next, loop backward through the values, looking
    ' for each item in turn.  If it's found, replace it
    For lngCount = lngNbrValues To 0 Step -1
        strFind = "\{" + CStr(lngCount) + "}"
        strX = Replace(strX, strFind, Values(lngCount))
        
        strFind = "\" + CStr(lngCount)
        strX = Replace(strX, strFind, Values(lngCount))
    Next lngCount

    strX = Replace(strX, "¥¥¥", "\")
    PrintF = strX
    On Error GoTo 0    
End Function

' And here's some code to test the function :

Public Function GenerateNewFilename(strFilename As String) As String
    Dim strNew As String
    Dim dteNow As Date
    
    dteNow = Now()

    strNew = PrintF("_\0\1\2_\3\4\5_" + strFilename, Format(CStr(Year(dteNow)), _
        "0000"), Format(CStr(Month(dteNow)), "00"), Format(CStr(Day(dteNow)), _
        "00"), Format(CStr(Hour(dteNow)), "00"), Format(CStr(Minute(dteNow)), _
        "00"), Format(CStr(Second(dteNow)), "00"))
   
    GenerateNewFilename = strNew
End Function

' Note: in VB.NET, the String.Format provides much more than this,
'  as it allows to specify how to format the values that replace the 
' placeholders.
Jeff Hill
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date