• Earn real money by being active: Hello Guest, earn real money by simply being active on the forum — post quality content, get reactions, and help the community. Once you reach the minimum credit amount, you’ll be able to withdraw your balance directly. Learn how it works.

Visual Basic Very Fast String Concatenation

Status
Not open for further replies.

Kaway

Banned
User
Joined
Aug 7, 2014
Messages
720
Reputation
0
Reaction score
5,766
Points
243
Credits
0
‎10 Years of Service‎
15%
Please note, if you want to make a deal with this user, that it is blocked.
[HIDE-THANKS]AppendStrings uses the WINAPI copymemory function to block-copy pre-allocated string buffers. MIDAppendStrings functions similar, but uses the MID$ function, which is safe for IIS COM components.

Usage: strResult = AppendStrings(strLargeString,strSmallString,strMediumString,.....)

Api.

Code:
>Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Ptr() As Any) As Long

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Code:
>Public Function AppendStrings( _
  strOriginal As String, _
  ParamArray varConcatString() As Variant) As String
   
  Dim i As Long
  Dim X As Long
  Dim lngTotal As Long
  Dim strBuffer As String
  Dim lngBufferSize As Long
  Dim lngLength As Long
  Dim lngPosition As Long
  
  If IsArray(varConcatString) = False Then
      AppendStrings = strOriginal
      Exit Function
  End If
  lngTotal = UBound(varConcatString)
  For i = 0 To lngTotal
     lngBufferSize = lngBufferSize + Len(varConcatString(i))
  Next i
  
  AppendStrings = Space$(lngBufferSize + Len(strOriginal))
  Mid$(AppendStrings, 1, Len(strOriginal)) = strOriginal
  lngBufferSize = Len(strOriginal) + 1
  
  For i = 0 To lngTotal
     lngLength = Len(varConcatString(i))
     If lngLength Then
        CopyMemory ByVal StrPtr(AppendStrings) + lngPosition, _
           ByVal StrPtr(strOriginal), LenB(strOriginal)
        lngPosition = lngPosition + LenB(strOriginal)
        
        CopyMemory ByVal StrPtr(AppendStrings) + lngPosition, _
           ByVal StrPtr(varConcatString(i)), LenB(varConcatString(i))
        lngPosition = lngPosition + LenB(varConcatString(i))
     End If
  Next i
End Function

Public Function MIDAppendStrings( _
  strOriginal As String, _
  ParamArray varConcatString() As Variant) As String
   
  Dim i As Long
  Dim X As Long
  Dim lngTotal As Long
  Dim strBuffer As String
  Dim lngBufferSize As Long
  Dim lngLength As Long
  
  If IsArray(varConcatString) = False Then
      MIDAppendStrings = strOriginal
      Exit Function
  End If
  lngTotal = UBound(varConcatString)
  For i = 0 To lngTotal
     lngBufferSize = lngBufferSize + Len(varConcatString(i))
  Next i
  
  MIDAppendStrings = Space$(lngBufferSize + Len(strOriginal))
  Mid$(MIDAppendStrings, 1, Len(strOriginal)) = strOriginal
  lngBufferSize = Len(strOriginal) + 1
  
  For i = 0 To lngTotal
     lngLength = Len(varConcatString(i))
     If lngLength Then
       Mid$(MIDAppendStrings, lngBufferSize, lngLength) = _
           varConcatString(i)
       lngBufferSize = lngBufferSize + lngLength
     End If
  Next i
  
End Function
[/HIDE-THANKS]

 
Status
Not open for further replies.
Back
Top