【问题标题】:Composite String Formatting (ie: using {0}, {1} and {2} in a string to format) in VB6VB6中的复合字符串格式化(即:在字符串中使用{0}、{1}和{2}进行格式化)
【发布时间】:2012-10-22 12:45:34
【问题描述】:

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

但是,我在任何地方都没有看到过这个用于 VB6 的内容,而且 google 也没有返回任何有用的信息。

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

在 VB.NET 中:

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

在 C# 中:

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

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

【问题讨论】:

    标签: c# .net vb.net string vb6


    【解决方案1】:

    这个函数应该做你想做的事

    '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
    

    【讨论】:

    • 谢谢,这肯定是我见过的执行复合函数格式化的最短函数(这是一件很棒的事情),因为我见过的时间长了大约 5-8 倍。投票赞成。
    【解决方案2】:

    在 VB6 中最接近 NET 复合格式的是运行时内置的 Format 函数。
    但是,它与提供相同的功能相去甚远。
    在我看来,除非你有非常简单的要求,否则你就不走运了。

    【讨论】:

      【解决方案3】:

      您最好从模拟 C/C++ 的角度进行思考,例如 sprintf。如果您在 google 上搜索“vb6 call sprintf”,这里有一些有用的文章,例如 one

      【讨论】:

        【解决方案4】:

        如果这不仅仅是 VB6 内置函数的学术问题,请替换和格式化。两者都没有 .NET Format 功能强大。您可以轻松滚动自己。为您编写自定义函数并将其添加到您重复使用的方法的 .bas 文件中。然后,您可以通过添加 .bas 文件将您喜欢的方法添加到项目中。这是一个可以使用的函数,类似于 .NET Format 函数。

        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
        

        例子:

        StringFormat("你好 {0}。我想见见 {1}。他们都为 {2} 工作。{0} 为 {2} 工作了 15 年。", "Bruce", "克里斯”、“凯尔”)

        这个和类似的答案都在这里,VBScript: What is the simplest way to format a string?

        【讨论】:

        • 感谢您的贡献,但是,以防万一您没有意识到,VB6 中不存在 RegExp obj,我认为您需要导入它或从参考列表,但是,您不需要在包中分发导入,因为它适用于任何具有 Internet Explorer 5.1(或 5.5)或更高版本的计算机。我忘记了需要考虑的引用的名称,并且不确定您是否已将上述内容写入该导入或基于内置的 .NET 支持编写上述内容。
        • 我在点击您另一篇文章的链接后发现,对库的引用是:Microsoft VBScript Regular Expressions 5.5。我猜它毕竟是 5.5(而不是 5.1)。
        • @Ethan-SoldMySoulToMicrosoft 我不记得参考了,因为我这样做只是作为另一个问题的快速示例,但它是在 VB6 中完成的。只是想向您展示编写自定义函数来完成您的要求很容易,尽管内置函数不存在。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-10
        • 1970-01-01
        • 2010-10-22
        • 1970-01-01
        相关资源
        最近更新 更多