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
| Class
|
Inherits From
|
Description
|
| Strings
|
Object
|
A string-list object. Mostly used as an abstract base class.
|
Properties
| 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
| 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
local strings = createStringList()
strings.add("First")
strings.add("Second")
strings.add("Third")
print("Count: " .. tostring(strings.Count))
print("First item: " .. strings[0])
strings.destroy()
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()
local strings = createStringList()
strings.Text = [[First line
Second line
Third line]]
print(strings.getText())
strings.destroy()
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()
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()
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.
Core Lua documentation entry points