Module:StringBuilder/doc: Difference between revisions
From Melvor Idle
(Created page with "== `StringBuilder` Module Documentation == The `StringBuilder` module provides functionality to efficiently build and manipulate strings in Lua. === Constructor === '''`StringBuilder.new()`''' Creates a new `StringBuilder` object. * '''Returns:''' `StringBuilder` object. === Methods === '''`StringBuilder:append(...)`''' Appends strings or values to the `StringBuilder` buffer. * '''Parameters:''' * `...`: One or more values to append to the buffer. '''`StringB...") |
No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== | == StringBuilder Module Documentation == | ||
The `StringBuilder` module provides functionality to efficiently build and manipulate strings in Lua. | The `StringBuilder` module provides functionality to efficiently build and manipulate strings in Lua. | ||
Line 5: | Line 5: | ||
=== Constructor === | === Constructor === | ||
====StringBuilder.new()==== | |||
Creates a new `StringBuilder` object. | Creates a new `StringBuilder` object. | ||
Line 13: | Line 13: | ||
=== Methods === | === Methods === | ||
====StringBuilder:append(...)==== | |||
Appends strings or values to the `StringBuilder` buffer. | Appends strings or values to the `StringBuilder` buffer. | ||
Line 20: | Line 20: | ||
* `...`: One or more values to append to the buffer. | * `...`: One or more values to append to the buffer. | ||
'''`StringBuilder: | ====StringBuilder:appendLine(...)==== | ||
Appends strings or values followed by a newline (`\n`) to the `StringBuilder` buffer. | |||
* '''Parameters:''' | |||
* `...`: One or more values to append to the buffer. | |||
====StringBuilder:clear()==== | |||
Clears the contents of the `StringBuilder` buffer. | |||
* '''Usage:''' Call this method to reset the `StringBuilder` buffer to an empty state. | |||
====StringBuilder:toString()==== | |||
Converts the `StringBuilder` buffer into a concatenated string. | Converts the `StringBuilder` buffer into a concatenated string. | ||
Line 27: | Line 40: | ||
=== Example Usage === | === Example Usage === | ||
To use the `StringBuilder` module in your Lua scripts, follow these examples: | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
Line 36: | Line 51: | ||
-- Append strings and values to the StringBuilder buffer | -- Append strings and values to the StringBuilder buffer | ||
sb:append("Hello, ") | sb:append("Hello, ") | ||
:append("world!") | |||
sb:append(123) | sb:append(123) | ||
-- Append a new line and more content | |||
sb:appendLine("This is a new line.") | |||
-- Convert StringBuilder buffer to a string | -- Convert StringBuilder buffer to a string | ||
Line 43: | Line 61: | ||
-- Output the result | -- Output the result | ||
print(result) -- Output: "Hello, world!123" | print(result) -- Output: "Hello, world!123\nThis is a new line." | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 21:43, 13 April 2024
StringBuilder Module Documentation
The `StringBuilder` module provides functionality to efficiently build and manipulate strings in Lua.
Constructor
StringBuilder.new()
Creates a new `StringBuilder` object.
- Returns: `StringBuilder` object.
Methods
StringBuilder:append(...)
Appends strings or values to the `StringBuilder` buffer.
- Parameters:
* `...`: One or more values to append to the buffer.
StringBuilder:appendLine(...)
Appends strings or values followed by a newline (`\n`) to the `StringBuilder` buffer.
- Parameters:
* `...`: One or more values to append to the buffer.
StringBuilder:clear()
Clears the contents of the `StringBuilder` buffer.
- Usage: Call this method to reset the `StringBuilder` buffer to an empty state.
StringBuilder:toString()
Converts the `StringBuilder` buffer into a concatenated string.
- Returns: Concatenated string representation of the `StringBuilder` buffer.
Example Usage
To use the `StringBuilder` module in your Lua scripts, follow these examples:
local StringBuilder = require("StringBuilder")
-- Create a new StringBuilder object
local sb = StringBuilder.new()
-- Append strings and values to the StringBuilder buffer
sb:append("Hello, ")
:append("world!")
sb:append(123)
-- Append a new line and more content
sb:appendLine("This is a new line.")
-- Convert StringBuilder buffer to a string
local result = sb:toString()
-- Output the result
print(result) -- Output: "Hello, world!123\nThis is a new line."