2

As explained here (Composite Format String: http://msdn.microsoft.com/en-us/library/txafckwd.aspx ) for VB.NET and C#.NET (.NET Framework).

However, I have not seen this for VB6 anywhere, and google didn't return anything useful.

Here is some sample code for .NET Framework (VB.NET and C#.NET) that I would like to do, but in VB6:

In VB.NET:

Dim myName As String = "Fred" 
String.Format("Name = {0}, hours = {1:hh}", myName, DateTime.Now)

In C#:

string myName = "Fred";
String.Format("Name = {0}, hours = {1:hh}", myName, DateTime.Now);

If anyone knows how to do this in VB6, or if it exists in some hidden corner of VB Classic, I would love to know. Thanks.

fr00ty_l00ps
  • 722
  • 3
  • 8
  • 22

4 Answers4

7

This function should do what you want

'Example:
Debug.Print FS("Name = {0}, Time = {1:hh:mm}, Number={2:#.00}", "My name", Now(), 12.5)

Function FS(strText As String, ParamArray values())
    Dim i As Integer
    i = 0

    For Each Value In values
        Dim intStart As Integer
        intStart = InStr(strText, "{" & i & "}")
        If intStart < 1 Then intStart = InStr(strText, "{" & i & ":")

        If intStart > 0 Then
            Dim intEnd As Integer
            intEnd = InStr(intStart, strText, "}")

            Dim strFormatedValue As String

            Dim intFormatPos As Integer
            intFormatPos = InStr(intStart, strText, ":")
            If intFormatPos < intEnd Then
                Dim strFormat As String
                strFormat = Mid(strText, intFormatPos + 1, intEnd - intFormatPos - 1)
                strFormatedValue = Format(Value, strFormat)
            Else
                strFormatedValue = Value
            End If

            strText = Left(strText, intStart - 1) & _
                      strFormatedValue & _
                      Mid(strText, intEnd + 1)

        End If
        i = i + 1
    Next

    FS = strText

End Function
Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108
Dan Terkildsen
  • 337
  • 2
  • 7
2

The thing that comes closest in VB6 to NET composite formatting is the Format function built in the runtime.
However, it 's very far from offering the same functionality.
In my opinion, unless you have very simple requirements, you are out of luck.

Steve
  • 213,761
  • 22
  • 232
  • 286
1

You will do better to think in terms of emulating C/C++, e.g., sprintf. There are some useful articles if you google for "vb6 call sprintf", e.g., such as this one.

Polyfun
  • 9,479
  • 4
  • 31
  • 39
0

If this isn't just an academic question built in VB6 functions are replace, and format. Neither are as powerful as the .NET Format function. You can easily roll you own. Write you custom function and add it to a .bas file of methods you use repeatedly. Then you can add your favorite methods to projects by adding the .bas file. Here is a function that can be used similar to the .NET Format function.

Public Function StringFormat(ByVal SourceString As String, ParamArray Arguments() As Variant) As String
   Dim objRegEx As RegExp  ' regular expression object
   Dim objMatch As Match   ' regular expression match object
   Dim strReturn As String ' the string that will be returned

   Set objRegEx = New RegExp
   objRegEx.Global = True
   objRegEx.Pattern = "(\{)(\d)(\})"

   strReturn = SourceString
   For Each objMatch In objRegEx.Execute(SourceString)
      strReturn = Replace(strReturn, objMatch.Value, Arguments(CInt(objMatch.SubMatches(1))))
   Next objMatch

   StringFormat = strReturn

End Function

Example:

StringFormat("Hello {0}. I'd like you to meet {1}. They both work for {2}. {0} has worked for {2} for 15 years.", "Bruce", "Chris", "Kyle")

This, and similar answers are here, VBScript: What is the simplest way to format a string?

Community
  • 1
  • 1
jac
  • 9,666
  • 2
  • 34
  • 63
  • Thank you for your contribution, however, just in case you didn't realize, the RegExp obj doesn't exist in VB6, I think you need to import it or create a reference to it from the references list, however, you don't need to distribute the import in your package because its available with any computer that has Internet Explorer 5.1 (or 5.5) or above. I forget the name of the reference that is needed thought, and not sure if you've written the above to that import or written the above based on in-built .NET support. – Ethan - SoldMySoulToMicrosoft Oct 24 '12 at 09:50
  • As I found after following the link to your other article, the reference to the library was this one: Microsoft VBScript Regular Expressions 5.5. I guess it was 5.5 after all (and not 5.1). – Ethan - SoldMySoulToMicrosoft Oct 24 '12 at 09:59
  • @Ethan-SoldMySoulToMicrosoft I don't remember the reference because I did this just as a quick sample to the other question, but it was done in VB6. Just trying to show you it is easy to write a custom function to accomplish what you were asking although a built in function doesn't exist. – jac Oct 24 '12 at 13:45