复合字符串格式化(即:在VB6中使用字符串中的{0},{1}和{2})

Eth*_*oft 2 .net c# vb.net string vb6

如此处所述(复合格式字符串:http://msdn.microsoft.com/en-us/library/txafckwd.aspx),用于VB.NET和C#.NET(.NET Framework).

但是,我还没有在VB6上看到这个,谷歌没有返回任何有用的东西.

以下是我想要做的.NET Framework(VB.NET和C#.NET)的一些示例代码,但在VB6中:

在VB.NET中:

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

在C#中:

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

如果有人知道如何在VB6中执行此操作,或者它存在于VB Classic的某个隐藏角落,我很想知道.谢谢.

小智 7

这个功能应该做你想要的

'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
Run Code Online (Sandbox Code Playgroud)