Difference between revisions of "Lua:Class:Strings"
Jump to navigation
Jump to search
(Major overhaul of the post.) |
m (→Examples: Syntax Highlight.) |
||
| Line 128: | Line 128: | ||
===Examples=== | ===Examples=== | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local strings = createStringList() | local strings = createStringList() | ||
| Line 139: | Line 139: | ||
strings.destroy() | strings.destroy() | ||
| − | </ | + | </syntaxhighlight> |
| − | < | + | <syntaxhighlight lang="lua" line> |
local strings = createStringList() | local strings = createStringList() | ||
| Line 155: | Line 155: | ||
strings.destroy() | strings.destroy() | ||
| − | </ | + | </syntaxhighlight> |
| − | < | + | <syntaxhighlight lang="lua" line> |
local strings = createStringList() | local strings = createStringList() | ||
| Line 167: | Line 167: | ||
strings.destroy() | strings.destroy() | ||
| − | </ | + | </syntaxhighlight> |
| − | < | + | <syntaxhighlight lang="lua" line> |
local strings = createStringList() | local strings = createStringList() | ||
| Line 183: | Line 183: | ||
strings.destroy() | strings.destroy() | ||
| − | </ | + | </syntaxhighlight> |
| − | < | + | <syntaxhighlight lang="lua" line> |
local strings = createStringList() | local strings = createStringList() | ||
| Line 198: | Line 198: | ||
strings.destroy() | strings.destroy() | ||
| − | </ | + | </syntaxhighlight> |
===Notes=== | ===Notes=== | ||
Latest revision as of 00:24, 27 June 2026
The Strings class represents a list of strings.
A Strings object inherits from Object. It is mostly an abstract base class and is commonly used by other objects that expose string-list behavior.
Contents
Inheritance[edit]
| Class | Inherits From | Description |
|---|---|---|
| Strings | Object | A string-list object. Mostly used as an abstract base class. |
Properties[edit]
| Property | Type | Description |
|---|---|---|
| LineBreak | String | The character or characters to count as a line break. |
| Text | String | All strings in the list as one string. |
| Count | Integer | The number of strings in the list. |
| String[] | String | Array access to a specific string in the list. |
| Data[] | Integer | Array access to the data value of a specific string in the list. |
| [] | String | Alias for String[]. |
Methods[edit]
| Method | Return Type | Description |
|---|---|---|
| clear() | void | Deletes all strings in the list. |
| add(string, data) | Integer | Adds a string to the list and optionally associates an integer data value with it. Returns the index of the added string. |
| addText(strings) | void | Adds multiple strings at once. |
| delete(index) | void | Deletes the string at the specified index. |
| getText() | String | Returns all strings as one large string. |
| setText(string) | void | Sets the strings of the object to the given text. The input can be multiline. |
| indexOf(string) | Integer | Returns the index of the specified string. Returns -1 if it is not found. |
| insert(index, string) | void | Inserts a string at the specified index and moves the following items down. |
| getCount() | Integer | Returns the number of strings in the list. |
| remove(string) | void | Removes the given string from the list. |
| loadFromFile(filename, ignoreencoding) | void | Loads the strings from a text file. If ignoreencoding is false, the file is loaded using the best encoding the loader can guess. The default for ignoreencoding is true. |
| saveToFile(filename) | void | Saves the strings to a text file. |
| getString(index) | String | Returns the string at the given index. |
| setString(index, string) | void | Replaces the string at the given index. |
| getData(index) | Integer | Returns the integer data value stored for the string at the given index. |
| setData(index, integer) | void | Sets the integer data value stored for the string at the given index. |
| beginUpdate() | void | Stops updates from triggering other events. This can prevent flashing while multiple changes are being made. |
| endUpdate() | void | Ends an update block started with beginUpdate(). |
Examples[edit]
1 local strings = createStringList()
2
3 strings.add("First")
4 strings.add("Second")
5 strings.add("Third")
6
7 print("Count: " .. tostring(strings.Count))
8 print("First item: " .. strings[0])
9
10 strings.destroy()
1 local strings = createStringList()
2
3 strings.add("Health", 100)
4 strings.add("Mana", 50)
5
6 print(strings.getString(0))
7 print(strings.getData(0))
8
9 strings.setData(0, 150)
10
11 print(strings.Data[0])
12
13 strings.destroy()
1 local strings = createStringList()
2
3 strings.Text = [[First line
4 Second line
5 Third line]]
6
7 print(strings.getText())
8
9 strings.destroy()
1 local strings = createStringList()
2
3 strings.beginUpdate()
4
5 for i = 1, 100 do
6 strings.add("Item " .. tostring(i))
7 end
8
9 strings.endUpdate()
10
11 print("Items added: " .. tostring(strings.Count))
12
13 strings.destroy()
1 local strings = createStringList()
2
3 strings.add("Line 1")
4 strings.add("Line 2")
5
6 strings.saveToFile("C:\\\\Temp\\\\strings.txt")
7 strings.clear()
8 strings.loadFromFile("C:\\\\Temp\\\\strings.txt")
9
10 print(strings.Text)
11
12 strings.destroy()
Notes[edit]
- Strings is mostly an abstract base class.
- Objects that expose string-list behavior may use this class or a descendant of it.
- Index-based access starts at 0.
- Use beginUpdate() and endUpdate() when making many changes at once.
- When using standalone string-list objects, destroy them when they are no longer needed.