API Reference
Here's the full list of everything you can do with StringBuilder!
Initialization
new
Returns a brand new StringBuilder object.
- Capacity: (Optional) The starting size in bytes. Defaults to
64. - MaxCapacity: (Optional) Absolute max capacity in bytes. Defaults to
1*1024*1024(1MB).
Modification
Append
Appends data (string, number, bool) at the end!
AppendLine
Same as Append, except it adds a line terminator for you at the end ("\n").
AppendFormat
Same as Append, except with formatting built-in!
AppendJoin
Concatenates and appends an array of string & numbers, placing your Separator right between each one (like commas).
As this uses table.concat, it will error if you pass anything other than strings and numbers!
AppendList
Concatenates and appends a list of string and/or number arguments. Way faster than chaining appends together!
As this uses table.concat, it will error if you pass anything other than strings and numbers!
Compare
Compares this StringBuilder with another one and returns true if their contents are fully identical!
GetCharacters
Grabs specific character(s) starting from your given Index. Count defaults to 1, but you can pass negative or positive numbers to grab more (negative values dont return reversed strings).
SetCharacters
Overwrites any character(s) starting at your Index directly with the new Data!
Insert
Inserts your Data right at the given Index and safely pushes everything else to the right.
Replace
Replaces all occurrences of Target with Replacement.
Remove
Chops exactly Count amount of letters right out of the string starting from Index.
Reverse
Reverses the string safely using utf8.graphemes. This is the default because it fully supports emojis and multi-language strings!
(Note: It's slower than ReverseBytes())
ReverseBytes
Reverses the string using raw buffer magic. Just as accurate as Reverse() for English letters and noticeably faster, but it lacks emoji and multi-language support. Use with caution!
Space
Just appends a space (" ") at the end.
Newline
Just appends a newline ("\n") at the end.
Clear
Clears the buffer! (More accurately, it just resets the end cursor, so it's basically free to call)
Capacity and Size
Capacity Details
Everything under the hood is buffers, so capacity and lengths are ALWAYS in bytes!
GetLength
Returns the current length of your string in bytes.
SetLength
Manually sets the length of the string!
- If it's smaller, it instantly truncates your string.- If it's larger than your current capacity, it forces a buffer expansion. Returns
true if successful, or false if your length is negative.
GetCapacity
Returns your current capacity in bytes.
EnsureCapacity
Makes sure your buffer is at least as big as EnsuredCapacity! Returns true if it had to/could resize, alongside your new capacity.
This is capped by your StringBuilder's max capacity, so be sure to check if the returned capacity is enough!
SetCapacity
Manually set the capacity safely! It resizes the buffer, and any out of bounds data just gets cut off. Returns the new actual capacity (capped by your max capacity!).
GetMaxCapacity
Returns the max capacity in bytes.
SetMaxCapacity
Manually sets your absolute max capacity. If you try to pass something less than your current capacity, it just silently ignores it.
GetFreeSpace
Returns your available writable space in bytes.
GetTotalFreeSpace
Returns your max writable space in bytes.
Extraction & Memory
ToString
Returns the fully built string!(You can also just use
tostring(stringBuilder) or print(stringBuilder))
Destroy
Destroys the object. Leaves the buffer to be cleaned up by the garbage collector automatically since there's no way to do it manually!
Packing & Compression
These are mostly for networking or datastores!
Pack
Packs the StringBuilder's data and metadata all together into a buffer!
Compress
Compresses the StringBuilder with Zstd. CompressionLevel is optional and must range from -7 to 22: higher means more compression at the cost of more time. Returns a packed and compressed buffer.
FromBuffer
Returns a StringBuilder object right back from a compressed (or packed) StringBuilder buffer. Returns nil if something is wrong.