StringArray Chilkat2 Java Reference Documentation

StringArray

Current Version: 11.7.0

Markdown version of this page for AI tools (v 11.7.0) · all AI docs

Chilkat.StringArray

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.

Object Creation

import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.StringArray;

try (StringArray stringArray = new StringArray()) {
    // ... use stringArray ...
} catch (ChilkatException e) {
    System.out.println(e.getMessage());          // "StringArray.someMethod failed: <reason>"
    System.out.println(e.getLastErrorText());    // the full log of the failed call
}
public StringArray()

Creates the underlying native Chilkat object. The native library (chilkat2java) is loaded the first time any Chilkat2 object is created; see the README in the download for where it is looked for (-Dchilkat2.library.path, a classpath resource, or java.library.path).

public void close()

Releases the native object (StringArray is AutoCloseable, so try-with-resources calls it). Calling it again does nothing; any other method or property accessor called afterwards throws IllegalStateException. An object that is never closed is freed by a background cleaner once it becomes unreachable, but the garbage collector does not see native memory, so closing is the normal path. Objects returned by methods are owned by the caller too.

public boolean isClosed()

Returns true once close() has been called.

Errors

A method that fails throws ChilkatException, an unchecked exception carrying getClassName(), getMethodName(), getReason() (one line, also in the message) and getLastErrorText() (the full log, captured at the moment of the failure). The object remains usable. Methods that look something up and find nothing return null instead of throwing (each such method says so), and methods that answer a question (hasMember, isUnlocked, ...) return a plain boolean. Property accessors do not throw for Chilkat reasons.

A null string or object argument throws NullPointerException naming the parameter (except where a method documents that null is accepted), and a string containing U+0000 throws IllegalArgumentException.

Use an object from one thread at a time; it may be handed from one thread to another. The one cross-thread operation is setting setAbortCurrent(true) on an object that is busy in another thread (on the classes that have AbortCurrent). Unlocking: call new Global().unlockBundle("...") once at startup.

Properties

Count
// read-only
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.

top
Crlf
// read/write
boolean getCrlf()
void setCrlf(boolean value)

If true, strings are always automatically converted to use CRLF line endings. If false, strings are automatically converted to use bare LF line endings.

top
DebugLogFilePath
// read/write
String getDebugLogFilePath()
void setDebugLogFilePath(String value)

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.

More Information and Examples
top
LastErrorHtml
// read-only
String getLastErrorHtml()

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.

top
LastErrorText
// read-only
String getLastErrorText()

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.

top
LastErrorXml
// read-only
String getLastErrorXml()

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.

top
LastMethodSuccess
// read/write
boolean getLastMethodSuccess()
void setLastMethodSuccess(boolean value)

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.

top
Length
// read-only
int getLength()

The number of strings contained within the internal collection. (Identical to the Count property.)

top
Trim
// read/write
boolean getTrim()
void setTrim(boolean value)

If true, whitespace, including carriage-returns and linefeeds, are automatically removed from the beginning and end of a string when added to the collection.

top
Unique
// read/write
boolean getUnique()
void setUnique(boolean value)

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.

top
VerboseLogging
// read/write
boolean getVerboseLogging()
void setVerboseLogging(boolean value)

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.

top
Version
// read-only
String getVersion()

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

Append
void append(String str)

Appends a string to the end of the internal ordered collection.

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
AppendSerialized
void appendSerialized(String encodedStr)

Appends multiple strings to the end of the internal ordered collection. The encodedStr is what is returned from the Serialize method (see below).

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
Clear
void clear()

Remove all strings from the internal collection.

top
Contains
boolean contains(String str)

Returns true if the string is present in the internal collection. The string comparisons are case sensitive.

Never throws: false is an answer, not a failure.

top
Find
int find(String findStr, int startIndex)

Finds the index of the first string equal to findStr. The search begins at startIndex. If the string is not found, -1 is returned. The first string in the array is at index 0.

top
FindFirstMatch
int findFirstMatch(String matchPattern, int startIndex)

Finds the first string that matches the matchPattern. The search begins at startIndex. If the string is not found, -1 is returned. The first string in the array is at index 0.

The matchPattern may contain zero or more asterisk chars, each of which matches 0 or more of any character.

top
GetString
String getString(int index)

Returns the string at an indexed location within the internal collection. The first string is located at index 0.

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
GetStringLen
int getStringLen(int index)

Returns Nth string's length, in characters. The first string is located at index 0.

top
InsertAt
void insertAt(int index, String str)

Inserts a string into the internal collection at a specified index. Using index 0 will insert at the beginning.

top
LastString
String lastString()

Returns the last string in the internal collection.

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
LoadFromFile
void loadFromFile(String 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.

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
LoadFromFile2
void loadFromFile2(String path, String 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.

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
LoadFromText
void loadFromText(String 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.

top
Pop
String pop()

Returns the last string and removes it from the internal collection.

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
Prepend
void prepend(String str)

Adds a string to the beginning of the internal collection.

top
Remove
void remove(String str)

Removes all strings equal to the string argument from the internal collection.

top
RemoveAt
void removeAt(int index)

Removes the string at a particular index.

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
ReplaceAt
void replaceAt(int index, String str)
Introduced in version 9.5.0.52

Replaces the string at a specified index.

top
SaveNthToFile
void saveNthToFile(int index, String saveToPath)

Saves the Nth string in the collection to a file.

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
SaveToFile
void saveToFile(String 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)

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
SaveToFile2
void saveToFile2(String saveToPath, String charset)

Saves the collection of strings to a file, one string per line. Strings are saved using the specified charset.

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
SaveToText
String saveToText()

Writes the collection of strings to a single string, one per line (separated by CRLF line endings).

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
Serialize
String 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.

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
Sort
void sort(boolean 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.

top
SplitAndAppend
void splitAndAppend(String str, String boundary)

Splits a string at a character or substring boundary and adds each resulting string to the internal collection.

top
StrAt
String strAt(int index)

Returns the string at a specific index.

Throws ChilkatException on failure; e.getLastErrorText() (or the object's getLastErrorText()) explains why, and the object remains usable.

top
Subtract
void subtract(StringArray stringArrayObj)

Subtracts the strings contained within stringArrayObj from the caller's internal collection.

top
Union
void union(StringArray stringArrayObj)

Performs the union set-operator. The result is that the caller will have a string collection that is the union of itself and stringArrayObj.

top