StringArray Lua Reference Documentation
chilkat.StringArray
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.StringArray
Store strings in an internal ordered collection and retrieve them by
zero-based index using
Append, prepend, insert, replace, remove, clear, or pop strings as the
collection changes.
Load one string per line from files or text, and save the collection back
to files or a single line-delimited string.
Use charset-specific load and save methods when working with UTF-8,
UTF-16, or other non-ANSI text encodings.
Check for exact strings, find values from a starting index, match
wildcard patterns, and sort ascending or descending.
Serialize the collection for later restoration, split text into entries,
and combine or subtract collections with set-style operations.
Manage an ordered collection of strings, with StringTable recommended for new code.
Chilkat.StringArray is an ordered string-collection class that
can append, prepend, insert, replace, remove, pop, and retrieve strings by
index. It can load one string per line from files or in-memory text, save the
collection back to text or files, enforce uniqueness, trim incoming values,
normalize line endings, search by exact value or wildcard pattern, sort
entries, split text into entries, serialize and restore the collection, and
perform set-style operations such as union and subtraction.
Chilkat.StringTable instead of
Chilkat.StringArray. StringTable provides a simpler,
faster, and more memory-efficient way to manage ordered lists of strings.
Use StringArray primarily when maintaining existing code or when
a specific API or older example already uses it.
Ordered string collection
GetString, StrAt, or
LastString.
Add and modify entries
Load and save text
Charset-aware file I/O
Search, match, and sort
Serialize and combine
StringArray, optionally set Trim,
Crlf, or Unique, then append strings directly or
load them from a file or text block. Search, sort, split, remove, union, or
subtract entries as needed, then save the result with
SaveToText, SaveToFile, or
SaveToFile2. For new code that only needs an ordered list of
strings, use Chilkat.StringTable instead.
Array,
Chilkat.StringArray is a Chilkat object that manages an ordered
string collection. It should not be confused with a native array type in a
programming language.
Object Creation
local chilkat = require("chilkat")
local string_array = chilkat.StringArray.new()Creates the object (chilkat.StringArray() does the same). Lua's garbage collector frees it when it is no longer referenced; string_array:dispose() frees it at once, and in Lua 5.4 and later local string_array <close> = chilkat.StringArray.new() frees it when the block ends. Calling a method of a disposed object raises an error.
Errors
A method that fails returns nil followed by the error text, as io.open does: local body, err = string_array:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.
Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.
Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (string_array.verbose_logging = true) and methods use the colon (string_array:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.
Properties
Count
local intVal = string_array.count
The number of strings contained within the object's internal array (i.e. ordered collection).
Important: This is an object that contains a collection of strings. Although the class/object name includes the word Array, it should not be confused with an array in the sense of the primitive array type used in a given programming language.
Crlf
local boolVal = string_array.crlf
string_array.crlf = boolVal
If true, strings are always automatically converted to use CRLF line endings. If false, strings are automatically converted to use bare LF line endings.
DebugLogFilePath
local strVal = string_array.debug_log_file_path
string_array.debug_log_file_path = strVal
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
LastErrorHtml
local strVal = string_array.last_error_html
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
local strVal = string_array.last_error_text
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
local strVal = string_array.last_error_xml
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
local boolVal = string_array.last_method_success
string_array.last_method_success = boolVal
Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
Length
local intVal = string_array.length
The number of strings contained within the internal collection. (Identical to the Count property.)
topTrim
local boolVal = string_array.trim
string_array.trim = boolVal
If true, whitespace, including carriage-returns and linefeeds, are automatically removed from the beginning and end of a string when added to the collection.
Unique
local boolVal = string_array.unique
string_array.unique = boolVal
If true, then duplicates are not allowed. When an attempt is made to insert a string that already exists, the duplicate insertion is silently suppressed and no error is returned. The default value is false.
VerboseLogging
local boolVal = string_array.verbose_logging
string_array.verbose_logging = boolVal
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
local strVal = string_array.version
Methods
Append
-- ok is true, or nil and err on failure
local ok, err = string_array:append(str)
Appends a string to the end of the internal ordered collection.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AppendSerialized
-- ok is true, or nil and err on failure
local ok, err = string_array:append_serialized(encoded_str)
Appends multiple strings to the end of the internal ordered collection. The encoded_str is what is returned from the Serialize method (see below).
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Clear
Remove all strings from the internal collection.
topContains
-- flag is true or false
local flag = string_array:contains(str)
Returns true if the string is present in the internal collection. The string comparisons are case sensitive.
Returns true or false: an answer, not a success flag.
Find
-- start_index is an integer
-- n is an integer
local n = string_array:find(find_str, start_index)
Finds the index of the first string equal to find_str. The search begins at start_index. If the string is not found, -1 is returned. The first string in the array is at index 0.
FindFirstMatch
-- start_index is an integer
-- n is an integer
local n = string_array:find_first_match(match_pattern, start_index)
Finds the first string that matches the match_pattern. The search begins at start_index. If the string is not found, -1 is returned. The first string in the array is at index 0.
The match_pattern may contain zero or more asterisk chars, each of which matches 0 or more of any character.
GetString
-- str is a string, or nil and err on failure
local str, err = string_array:get_string(index)
Returns the string at an indexed location within the internal collection. The first string is located at index 0.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetStringLen
-- n is an integer
local n = string_array:get_string_len(index)
Returns Nth string's length, in characters. The first string is located at index 0.
topInsertAt
-- str is a string
string_array:insert_at(index, str)
Inserts a string into the internal collection at a specified index. Using index 0 will insert at the beginning.
topLastString
local str, err = string_array:last_string()
Returns the last string in the internal collection.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
LoadFromFile
-- ok is true, or nil and err on failure
local ok, err = string_array:load_from_file(path)
Loads strings from a file (one per line) into the internal collection. It is assumed the file contains ANSI strings. To load from a file containing non-ANSI strings (such as utf-8), call LoadFromFile2 instead.
Note: This method appends the strings in path to the existing collection of strings contained in this object.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
LoadFromFile2
-- charset is a string
-- ok is true, or nil and err on failure
local ok, err = string_array:load_from_file2(path, charset)
Loads strings from a file (one per line) into the internal collection. The charset specifies the character encoding (such as utf-8) of the strings contained in the file.
Note: This method appends the strings in path to the existing collection of strings contained in this object.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
LoadFromText
string_array:load_from_text(str)
Loads strings from an in-memory string (one per line) into the internal collection.
Note: This method appends the strings in str to the existing collection of strings contained in this object.
Pop
local str, err = string_array:pop()
Returns the last string and removes it from the internal collection.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Prepend
string_array:prepend(str)
Adds a string to the beginning of the internal collection.
topRemove
string_array:remove(str)
Removes all strings equal to the string argument from the internal collection.
topRemoveAt
-- ok is true, or nil and err on failure
local ok, err = string_array:remove_at(index)
Removes the string at a particular index.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
ReplaceAt
-- str is a string
string_array:replace_at(index, str)
Replaces the string at a specified index.
topSaveNthToFile
-- save_to_path is a string
-- ok is true, or nil and err on failure
local ok, err = string_array:save_nth_to_file(index, save_to_path)
Saves the Nth string in the collection to a file.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SaveToFile
-- ok is true, or nil and err on failure
local ok, err = string_array:save_to_file(path)
Saves the collection of strings to a file, one string per line. Strings are saved using the ANSI charset. (Call SaveToFile2 to specify a charset, such as utf-8)
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SaveToFile2
-- charset is a string
-- ok is true, or nil and err on failure
local ok, err = string_array:save_to_file2(save_to_path, charset)
Saves the collection of strings to a file, one string per line. Strings are saved using the specified charset.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SaveToText
local str, err = string_array:save_to_text()
Writes the collection of strings to a single string, one per line (separated by CRLF line endings).
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Serialize
local str, err = string_array:serialize()
Returns an string which is an encoded representation of all the strings in the collection. The string collection can be re-created by calling the AppendSerialized method.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Sort
string_array:sort(ascending)
Sorts the strings in the collection in ascending or descending order. To sort in ascending order, set ascending to true, otherwise set ascending equal to false.
SplitAndAppend
-- boundary is a string
string_array:split_and_append(str, boundary)
Splits a string at a character or substring boundary and adds each resulting string to the internal collection.
topStrAt
-- str is a string, or nil and err on failure
local str, err = string_array:str_at(index)
Returns the string at a specific index.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Subtract
string_array:subtract(string_array_obj)
Subtracts the strings contained within string_array_obj from the caller's internal collection.
Union
string_array:union(string_array_obj)
Performs the union set-operator. The result is that the caller will have a string collection that is the union of itself and string_array_obj.