使用预制的Stringbuilder类 [英] Using Pre-Made Stringbuilder Class

查看:67
本文介绍了使用预制的Stringbuilder类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何在VBA中使用stringbuilder函数,但是在VBA中找不到如何使用它们的资源时遇到了麻烦.我可以使用一些帮助来使用预制的stringbuilder类.

I am trying to learn how to use stringbuilder functions in VBA, but am having trouble finding resources in VBA on how to use them. I could use some help to use a pre-made stringbuilder class.

我确实知道这些子程序中的每一个在编写最终代码中都起着一定的作用.例如,我看过其他使用"string.append"的示例,但是我不确定在这种情况下它是否可以工作.我可以对如何利用此代码有一些见识.请帮忙!

I do know that each of these subs play some role in writing a final code. For example, I have seen other examples that use "string.append" but I am not sure if that is how it works in this case. I could use a little bit of insight into how to utilize this code. Please help!

让我理解的最好方法是,是否有人可以使用此示例stringbuilder类将几行示例代码放在一起.谢谢!!

The best way for me to understand is if someone can throw together a few lines of sample code using this example stringbuilder class. Thank you!!

Private m_arrBuffer
Private m_strDelimiter
Private Sub Class_Initialize()
    m_arrBuffer = Array()
    m_strDelimiter = ""
End Sub

Private Sub Class_Terminate()
    m_arrBuffer = Empty
End Sub

Public Property Get Delimiter()
    Delimiter = m_strDelimiter
End Property

Public Property Let Delimiter(strDelimiter)
    m_strDelimiter = strDelimiter
End Property

Public Sub Append(strValue)
    ReDim Preserve m_arrBuffer(UBound(m_arrBuffer) + 1)
    m_arrBuffer(UBound(m_arrBuffer)) = strValue
End Sub

Public Sub AppendLine(strValue)
    Me.Append strValue & vbCrLf
End Sub

Public Sub Compact()
    If Not Me.Delimiter = "" Then
        strOriginalDelimiter = Me.Delimiter
        Me.Delimiter = ""
    End If
    strTemp = Me.ToString
    m_arrBuffer = Array()
    Me.Append strTemp
    Me.Delimiter = strOriginalDelimiter
End Sub

Public Function ToArray()
    ToArray = m_arrBuffer
End Function

Public Function ToString()
    ToString = Join(m_arrBuffer, m_strDelimiter)
End Function

推荐答案

众所周知,VBA中的字符串连接(&)速度很慢,因此,如果您拥有您需要组合大量的字符串.

String concatenation (&) in VBA is notoriously slow, so often "stringbuilder" classes like this one are used to speed up the process if you have a large number of strings you need to combine.

通常的想法是使用Array()存储单个字符串成分,然后在需要时使用Join()函数组合所有字符串一次.添加字符串后,阵列会自动调整大小.许多人使用"GrowBy"功能(尽管此功能没有),在达到数组限制时以静态大小或因子增长数组.这也可以提高性能,因为每次插入字符串都调用ReDim Preserve可能会造成很大的损失.

The general idea is to use an Array() to store individual string components and then combine all strings once, when you need them, using the Join() function. The array is automatically resized as strings are added. Many use a "GrowBy" feature (although this one doesn't) to grow the array by a static size or factor as the array limit is reached. That can improve performance as well, since calling ReDim Preserve for every string insertion can take its toll.

要回答您的问题,请假装您需要构建HTML文件的一部分.您可以像这样使用显示的字符串类:

To answer your question, pretend you needed to build a portion of an HTML file. You could use the shown string class like so:

Dim sb
Set sb = New StringBuilder      ' Guessing here. You haven't shown the class name.
sb.Append "some string"
sb.Append "another string"
sb.Append "a third string"
....
sb.Delimiter = "<br>"
myHtmlFile.Write sb.ToString()

将打印以下内容:

some string<br>another string<br>a third string

这是一般想法.通过使用数组,尽可能避免使用&,您应该会看到一些显着的性能改进.

That's the general idea. Avoid the use of & as much as possible by using an array and you should see some significant performance improvements.

这篇关于使用预制的Stringbuilder类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆