Difference between revisions of "Lua:Class:Strings"
Jump to navigation
Jump to search
(filled out a proper class template for the Strings class) |
m (→Examples: Syntax Highlight.) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Lua]] | [[Category:Lua]] | ||
| − | + | {{Class|'''class''' Strings ''':''' Object}} | |
| − | + | 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. | |
| − | |||
| − | |||
| − | ; | + | ===Inheritance=== |
| − | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | |
| + | !align="left"|Class | ||
| + | !align="left"|Inherits From | ||
| + | !style="width: 80%;background-color:white;" align="left"|Description | ||
| + | |- | ||
| + | |Strings | ||
| + | |Object | ||
| + | |A string-list object. Mostly used as an abstract base class. | ||
| + | |} | ||
| − | ; String[] | + | ===Properties=== |
| − | + | {|width="100%" cellpadding="10%" cellspacing="0" border="0" | |
| + | !align="left"|Property | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|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 == | + | ===Methods=== |
| − | ; clear() | + | {|width="100%" cellpadding="10%" cellspacing="0" border="0" |
| − | + | !align="left"|Method | |
| + | !align="left"|Return Type | ||
| + | !style="width: 70%;background-color:white;" align="left"|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=== | |
| − | + | <syntaxhighlight lang="lua" line> | |
| + | local strings = createStringList() | ||
| − | + | strings.add("First") | |
| − | + | strings.add("Second") | |
| + | strings.add("Third") | ||
| − | + | print("Count: " .. tostring(strings.Count)) | |
| − | : | + | print("First item: " .. strings[0]) |
| − | + | strings.destroy() | |
| − | + | </syntaxhighlight> | |
| − | + | <syntaxhighlight lang="lua" line> | |
| − | + | local strings = createStringList() | |
| − | |||
| − | + | strings.add("Health", 100) | |
| − | + | strings.add("Mana", 50) | |
| − | + | print(strings.getString(0)) | |
| − | + | print(strings.getData(0)) | |
| − | + | strings.setData(0, 150) | |
| − | |||
| − | + | print(strings.Data[0]) | |
| − | |||
| − | + | strings.destroy() | |
| − | + | </syntaxhighlight> | |
| − | + | <syntaxhighlight lang="lua" line> | |
| − | + | local strings = createStringList() | |
| − | + | strings.Text = [[First line | |
| − | : | + | Second line |
| + | Third line]] | ||
| + | |||
| + | print(strings.getText()) | ||
| + | |||
| + | strings.destroy() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | <syntaxhighlight lang="lua" line> | ||
| + | local strings = createStringList() | ||
| + | |||
| + | strings.beginUpdate() | ||
| + | |||
| + | for i = 1, 100 do | ||
| + | strings.add("Item " .. tostring(i)) | ||
| + | end | ||
| + | |||
| + | strings.endUpdate() | ||
| + | |||
| + | print("Items added: " .. tostring(strings.Count)) | ||
| + | |||
| + | strings.destroy() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | <syntaxhighlight lang="lua" line> | ||
| + | local strings = createStringList() | ||
| + | |||
| + | strings.add("Line 1") | ||
| + | strings.add("Line 2") | ||
| + | |||
| + | strings.saveToFile("C:\\\\Temp\\\\strings.txt") | ||
| + | strings.clear() | ||
| + | strings.loadFromFile("C:\\\\Temp\\\\strings.txt") | ||
| + | |||
| + | print(strings.Text) | ||
| + | |||
| + | strings.destroy() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Notes=== | ||
| + | * 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. | ||
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
| − | |||
| − | |||
| − | |||
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.