Results 1 to 40 of 72

Thread: [VB6] StringBuilder - Fast string concatenation

Threaded View

  1. #1

    Thread Starter
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine on fire (country of slaves)
    Posts
    750

    [VB6] StringBuilder - Fast string concatenation

    v.2.6.
    It's well-tested class for concatenating strings like:
    Code:
    s = s & "one line"
    s = s & "two line"
    ...
    s = s & "N line"
    but much much more faster than VB runtime do it.

    Cases when it is needed sometimes:
    Well, e.g., I'm using it to prepare some really huge logs of program in a single 'String' variable (e.g. up to 1 MB.) rather then writing them line by line to file (for performance acceleration purposes, if debug mode of my app. is disabled).
    Don't know what else good cases. Better, don't store large data in this way, use suitable tools: arrays, databases ...

    Using examples:
    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        'init 
        Dim sb As clsStringBuilder
        Set sb = New clsStringBuilder
    
        'concatenation 
        sb.Append "Very "
        sb.Append "many "
        sb.Append "pieces "
        sb.Append "of data"
    
        'result 
        Debug.Print "All data: "; sb.ToString
        Debug.Print "Len of data: "; sb.length
    
        'removing 5 characters from position # 1 
        sb.Remove 1, 5
        Debug.Print "New data: "; sb.ToString
    
        'inserting string in position # 1 
        sb.Insert 1, "Not "
        Debug.Print "New data: "; sb.ToString
    
        'overwrite part of the text from position # 1 (same like MID(str,x) = "...") 
        sb.Overwrite 1, "How"
        Debug.Print "New data: "; sb.ToString
    
        'getting 2 first characters 
        Debug.Print "2 left chars: "; sb.ToStringLeft(2)
    
        'getting 2 characters from the end 
        Debug.Print "2 end chars: "; sb.ToStringRight(2)
    
        'getting 2 characters from the middle, beginning from position 6 
        Debug.Print "2 middle chars: "; sb.ToStringMid(6, 2)
    
        'getting a pointer to a NUL terminated string to use somehow (e.g. write on disk by ptr, WriteFile, e.t.c.) 
        'this method is much faster than .ToString() 
        'warning: you should use this pointer before calling next any method of StringBuilder, that may cause changing its data 
        Debug.Print "ptr to string: "; sb.ToStringPtr
    
        'replacing the data (same as Clear + Append) 
        sb.StringData = "Anew"
        Debug.Print "New data: "; sb.ToString
    
        'go to the next line (append CrLf) 
        sb.AppendLine ""
        'append second line with CrLf at the end 
        sb.AppendLine "Second line"
        'append third line without CrLf 
        sb.Append "Third Line"
    
        Debug.Print sb.ToString
    
        'clear all data 
        sb.Clear
        Debug.Print "Len of data (after clear): "; sb.length
    
        'Search samples (by default search is case sensitive) 
        'Set new data 
        sb.StringData = "|textile|Some|text|to|search"
        Debug.Print "New data: "; sb.ToString
    
        'Simple search ('text' will be found inside 'textile' word) 
        Debug.Print "Position of 'text': " & sb.Find(1, "text")
    
        'Simple search (start search from position 3) 
        Debug.Print "Position of 'text': " & sb.Find(3, "text")
    
        Debug.Print "'some' (case sensitive): " & sb.Find(1, "some")
        Debug.Print "'some' (case insensitive): " & sb.Find(1, "some", , vbTextCompare)
    
        'Search by delimiter 
        Debug.Print "searching for |text|: " & sb.Find(1, "text", "|")
    
        'Search for empty string, saved with delimiter | 
        Debug.Print "empty string (delim = '|'): " & sb.Find(1, "", "|")
    
        'Undo operations 
        sb.StringData = "Some data "
        Debug.Print "Orig. string: " & sb.ToString
    
        sb.Append "remove"
        Debug.Print "After Append: " & sb.ToString
        'or you can use .UndoAppend 
        sb.Undo
        Debug.Print "After Undo:   " & sb.ToString
    
        sb.Insert 6, "bad "
        Debug.Print "After Insert: " & sb.ToString
        'or you can use .UndoInsert 
        sb.Undo
        Debug.Print "After Undo:   " & sb.ToString
    
        sb.Overwrite 1, "Here"
        Debug.Print "After Overwrite: " & sb.ToString
        'or you can use .UndoOverwrite 
        sb.Undo
        Debug.Print "After Undo:      " & sb.ToString
    
        sb.Remove 2, 5
        Debug.Print "After Remove: " & sb.ToString
        'or you can use .UndoRemove 
        sb.Undo
        Debug.Print "After Undo:   " & sb.ToString
    
        'when you finished work with the class 
        Set sb = Nothing
    
        Unload Me
    End Sub
    
    
    Result:
    All data: Very many pieces of data
    Len of data: 24
    New data: many pieces of data
    New data: Not many pieces of data
    New data: How many pieces of data
    2 left chars: Ho
    2 end chars: ta
    2 middle chars: an
    ptr to string: 228655136
    New data: Anew
    Anew
    Second line
    Third Line
    Len of data (after clear): 0
    New data: |textile|Some|text|to|search
    Position of 'text': 2
    Position of 'text': 15
    'some' (case sensitive): 0
    'some' (case insensitive): 10
    searching for |text|: 15
    empty string (delim = '|'): 1
    Orig. string: Some data
    After Append: Some data remove
    After Undo: Some data
    After Insert: Some bad data
    After Undo: Some data
    After Overwrite: Here data
    After Undo: Some data
    After Remove: Sata
    After Undo: Some data
    Copyrights: VolteFace (Fork by Dragokas)
    Attached Files Attached Files
    Last edited by Dragokas; Oct 2nd, 2023 at 11:56 AM. Reason: new update
    Malware analyst, VirusNet developer, HiJackThis+ author || my CodeBank works

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width