Latest version of the StringBuffer class
This is the latest version of my StringBuffer class. You can use it in the same way you use dynamic arrays in JavaScript.
The code
Class StringBuffer
Public count As Integer
Private arr() As String
Private size As Integer
Private increment As Integer
Sub New(Byval a As Integer)
Redim arr(a)
increment=a
count=0
End Sub
Sub add(Byval a As String)
size=Ubound(arr())
If count>=size Then
Redim Preserve arr(size+increment)
End If
arr(count)=a
count=count+1
End Sub
Function collapse(Byval a As String) As String
Dim sTemp As String
If count>0 Then
Redim Preserve arr(count-1)
sTemp=Join(arr, a)
End If
collapse=sTemp
End Function
Function getArray() As Variant
If count>0 Then
Redim Preserve arr(count-1)
Else
Redim arr(0)
End If
getArray=arr
End Function
Function clear
count=0
End Function
End Class
Sample use
Function getField(Byval label As String, Byval fieldName As String)As String
Dim tmp As New Stringbuffer(5)
Dim value As String
value= Join(doc.GetItemValue(fieldName), ", ")
tmp.add |<li>|
tmp.add |<label for="f| & fieldName & |">| & label & |</label>|
tmp.add |<input id="f| & fieldName &|" name="| & fieldName & |" value="| & value & |" />|
tmp.add |</li>|
getField=tmp.collapse("")
End Function
Comments
11/21/2007 02:02:54 PM, Tom ONeil
I'm confused as to why you would call this Stringbuffer? Wouldn't it be confused with the Java implementation?
11/22/2007 10:10:11 AM, Albin Theander
Tom: I think calling it a StringBuffer is good, since it provides the same functionality.
Michel: To make it even more in line with the java StringBuffer, maybe the "add" method should be called "append"?
11/22/2007 10:40:02 AM, Michel Van der Meiren
@Albin: I agree. I have chosen "add" because i'ts shorter and I hate typing. I Named it "StringBuffer" since this was the purpose of it in the first place: concatenating strings without recreating a new instance of it in memory all the time. @Tom: I like to flirt with Java names, as does JavaScript as well, e.g. its try{}catch(e){} function.
11/29/2007 08:23:12 PM, Charles Robinson
Have you seen Julian Robichaux's version: http://nsftools.com/tips/StringBuffer.lss . One big difference is yours is limited to 32K. Julian's can go up to 2GB. I'm not sure he checks for the upper limit, though.
30/11/2007 08:32:46, Michel Van der Meiren
@Charles: What's the difference between mine and Julian's? Is there a limit on string size in LotusScript?
11/30/2007 05:23:35 PM, Charles Robinson
Compare your add method to Julian's append. When he hits the maximum number of array entries (which he has as a variable, but LotusScript has an internal limit of 32K) he writes the entire array out to a temp string and resets the array.
What happens if you try to put 40,000 strings together with your StringBuffer? You'll get an error when you try to Redim the array to larger than 32K.
The limit of a string in LotusScript is 2GB. http://www.geniisoft.com/showcase.nsf/DominoLimits
01/12/2007 11:24:38, Michel Van der Meiren
Great. Now I understand. At present, I don't have the problem, but I will certainly keep this in mind. Thanks.
To add a comment, log in or register as new user. It's free and safe.