Class StringArray
- All Implemented Interfaces:
AutoCloseable
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.
Recommendation:
For new development, prefer 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
Store strings in an internal ordered collection and retrieve them by
zero-based index using GetString, StrAt, or
LastString.
Add and modify entries
Append, prepend, insert, replace, remove, clear, or pop strings as the collection changes.
Load and save text
Load one string per line from files or text, and save the collection back to files or a single line-delimited string.
Charset-aware file I/O
Use charset-specific load and save methods when working with UTF-8, UTF-16, or other non-ANSI text encodings.
Search, match, and sort
Check for exact strings, find values from a starting index, match wildcard patterns, and sort ascending or descending.
Serialize and combine
Serialize the collection for later restoration, split text into entries, and combine or subtract collections with set-style operations.
Common pattern:
Create a 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.
Naming note:
Although the class name contains 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.
</section>
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAppends a string to the end of the internal ordered collection.voidappendSerialized(String encodedStr)Appends multiple strings to the end of the internal ordered collection.voidclear()Remove all strings from the internal collection.booleanReturnstrueif the string is present in the internal collection.intFinds the index of the first string equal tofindStr.intfindFirstMatch(String matchPattern, int startIndex)Finds the first string that matches thematchPattern.intgetCount()The number of strings contained within the object's internal array (i.e.booleangetCrlf()Iftrue, strings are always automatically converted to use CRLF line endings.intThe number of strings contained within the internal collection.getString(int index)Returns the string at an indexed location within the internal collection.intgetStringLen(int index)Returns Nth string's length, in characters.booleangetTrim()Iftrue, whitespace, including carriage-returns and linefeeds, are automatically removed from the beginning and end of a string when added to the collection.booleanIftrue, then duplicates are not allowed.voidInserts a string into the internal collection at a specified index.Returns the last string in the internal collection.voidloadFromFile(String path)Loads strings from a file (one per line) into the internal collection.voidloadFromFile2(String path, String charset)Loads strings from a file (one per line) into the internal collection.voidloadFromText(String str)Loads strings from an in-memory string (one per line) into the internal collection.pop()Returns the last string and removes it from the internal collection.voidAdds a string to the beginning of the internal collection.voidRemoves all strings equal to the string argument from the internal collection.voidremoveAt(int index)Removes the string at a particular index.voidReplaces the string at a specified index.voidsaveNthToFile(int index, String saveToPath)Saves the Nth string in the collection to a file.voidsaveToFile(String path)Saves the collection of strings to a file, one string per line.voidsaveToFile2(String saveToPath, String charset)Saves the collection of strings to a file, one string per line.Writes the collection of strings to a single string, one per line (separated by CRLF line endings).Returns an string which is an encoded representation of all the strings in the collection.voidsetCrlf(boolean value)Sets theCrlfproperty; seegetCrlf().voidsetTrim(boolean value)Sets theTrimproperty; seegetTrim().voidsetUnique(boolean value)Sets theUniqueproperty; seegetUnique().voidsort(boolean ascending)Sorts the strings in the collection in ascending or descending order.voidsplitAndAppend(String str, String boundary)Splits a string at a character or substring boundary and adds each resulting string to the internal collection.strAt(int index)Returns the string at a specific index.voidsubtract(StringArray stringArrayObj)Subtracts the strings contained withinstringArrayObjfrom the caller's internal collection.voidunion(StringArray stringArrayObj)Performs the union set-operator.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
StringArray
public StringArray()Creates a newStringArrayobject.
-
-
Method Details
-
getCount
public int getCount()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.- Returns:
- the value of the
Countproperty (read-only)
-
getCrlf
public boolean getCrlf()Iftrue, strings are always automatically converted to use CRLF line endings. Iffalse, strings are automatically converted to use bare LF line endings.- Returns:
- the value of the
Crlfproperty
-
setCrlf
public void setCrlf(boolean value)Sets theCrlfproperty; seegetCrlf().- Parameters:
value- the new value
-
getLength
public int getLength()The number of strings contained within the internal collection. (Identical to the Count property.)- Returns:
- the value of the
Lengthproperty (read-only)
-
getTrim
public boolean getTrim()Iftrue, whitespace, including carriage-returns and linefeeds, are automatically removed from the beginning and end of a string when added to the collection.- Returns:
- the value of the
Trimproperty
-
setTrim
public void setTrim(boolean value)Sets theTrimproperty; seegetTrim().- Parameters:
value- the new value
-
getUnique
public boolean getUnique()Iftrue, 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 isfalse.- Returns:
- the value of the
Uniqueproperty
-
setUnique
public void setUnique(boolean value)Sets theUniqueproperty; seegetUnique().- Parameters:
value- the new value
-
append
Appends a string to the end of the internal ordered collection.- Parameters:
str- thestrargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendSerialized
Appends multiple strings to the end of the internal ordered collection. TheencodedStris what is returned from the Serialize method (see below).- Parameters:
encodedStr- theencodedStrargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
clear
public void clear()Remove all strings from the internal collection. -
contains
Returnstrueif the string is present in the internal collection. The string comparisons are case sensitive.- Parameters:
str- thestrargument (notnull)- Returns:
- the result; this method never throws
ChilkatException
-
find
Finds the index of the first string equal tofindStr. The search begins atstartIndex. If the string is not found, -1 is returned. The first string in the array is at index 0.- Parameters:
findStr- thefindStrargument (notnull)startIndex- thestartIndexargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
findFirstMatch
Finds the first string that matches thematchPattern. The search begins atstartIndex. If the string is not found, -1 is returned. The first string in the array is at index 0.The
matchPatternmay contain zero or more asterisk chars, each of which matches 0 or more of any character.- Parameters:
matchPattern- thematchPatternargument (notnull)startIndex- thestartIndexargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getString
Returns the string at an indexed location within the internal collection. The first string is located at index 0.- Parameters:
index- theindexargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getStringLen
public int getStringLen(int index)Returns Nth string's length, in characters. The first string is located at index 0.- Parameters:
index- theindexargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
insertAt
Inserts a string into the internal collection at a specified index. Using index 0 will insert at the beginning.- Parameters:
index- theindexargumentstr- thestrargument (notnull)
-
lastString
Returns the last string in the internal collection.- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
loadFromFile
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
pathto the existing collection of strings contained in this object.- Parameters:
path- thepathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
loadFromFile2
Loads strings from a file (one per line) into the internal collection. Thecharsetspecifies the character encoding (such as utf-8) of the strings contained in the file.Note: This method appends the strings in
pathto the existing collection of strings contained in this object.- Parameters:
path- thepathargument (notnull)charset- thecharsetargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
loadFromText
Loads strings from an in-memory string (one per line) into the internal collection.Note: This method appends the strings in
strto the existing collection of strings contained in this object.- Parameters:
str- thestrargument (notnull)
-
pop
Returns the last string and removes it from the internal collection.- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
prepend
Adds a string to the beginning of the internal collection.- Parameters:
str- thestrargument (notnull)
-
remove
Removes all strings equal to the string argument from the internal collection.- Parameters:
str- thestrargument (notnull)
-
removeAt
public void removeAt(int index)Removes the string at a particular index.- Parameters:
index- theindexargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
replaceAt
Replaces the string at a specified index.- Parameters:
index- theindexargumentstr- thestrargument (notnull)
-
saveNthToFile
Saves the Nth string in the collection to a file.- Parameters:
index- theindexargumentsaveToPath- thesaveToPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
saveToFile
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 asutf-8)- Parameters:
path- thepathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
saveToFile2
Saves the collection of strings to a file, one string per line. Strings are saved using the specifiedcharset.- Parameters:
saveToPath- thesaveToPathargument (notnull)charset- thecharsetargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
saveToText
Writes the collection of strings to a single string, one per line (separated by CRLF line endings).- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
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 result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
sort
public void sort(boolean ascending)Sorts the strings in the collection in ascending or descending order. To sort in ascending order, setascendingtotrue, otherwise setascendingequal tofalse.- Parameters:
ascending- theascendingargument
-
splitAndAppend
Splits a string at a character or substring boundary and adds each resulting string to the internal collection.- Parameters:
str- thestrargument (notnull)boundary- theboundaryargument (notnull)
-
strAt
Returns the string at a specific index.- Parameters:
index- theindexargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
subtract
Subtracts the strings contained withinstringArrayObjfrom the caller's internal collection.- Parameters:
stringArrayObj- thestringArrayObjargument (notnull)
-
union
Performs the union set-operator. The result is that the caller will have a string collection that is the union of itself andstringArrayObj.- Parameters:
stringArrayObj- thestringArrayObjargument (notnull)
-