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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13011747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:01:05  来源:igfitidea点击:

Composite String Formatting (ie: using {0}, {1} and {2} in a string to format) in VB6

c#.netvb.netstringvb6

提问by Ethan - SoldMySoulToMicrosoft

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

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

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

但是,我在任何地方都没有在 VB6 中看到过这个,谷歌也没有返回任何有用的东西。

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

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

In VB.NET:

在 VB.NET 中:

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

In C#:

在 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.

如果有人知道如何在 VB6 中做到这一点,或者它是否存在于 VB Classic 的某个隐藏角落,我很想知道。谢谢。

回答by Dan Terkildsen

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

回答by Steve

The thing that comes closest in VB6 to NET composite formatting is the Formatfunction 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.

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

回答by Polyfun

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.

您最好考虑模拟 C/C++,例如 sprintf。如果您在 google 上搜索“vb6 call sprintf”,可以找到一些有用的文章,例如这一篇

回答by jac

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.

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

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")

StringFormat("你好 {0}。我想让你认识一下 {1}。他们都为 {2} 工作。{0} 已经为 {2} 工作了 15 年。", "Bruce", "Chris", “凯尔”)

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

这个和类似的答案在这里,VBScript:格式化字符串的最简单方法是什么?