Class StringArray

java.lang.Object
com.chilkatsoft.chilkat2.ChilkatObject
com.chilkatsoft.chilkat2.StringArray
All Implemented Interfaces:
AutoCloseable

public class StringArray extends ChilkatObject
<section class="stringarray-summary">

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
    Constructor
    Description
    Creates a new StringArray object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    append​(String str)
    Appends a string to the end of the internal ordered collection.
    void
    appendSerialized​(String encodedStr)
    Appends multiple strings to the end of the internal ordered collection.
    void
    Remove all strings from the internal collection.
    boolean
    contains​(String str)
    Returns true if the string is present in the internal collection.
    int
    find​(String findStr, int startIndex)
    Finds the index of the first string equal to findStr.
    int
    findFirstMatch​(String matchPattern, int startIndex)
    Finds the first string that matches the matchPattern.
    int
    The number of strings contained within the object's internal array (i.e.
    boolean
    If true, strings are always automatically converted to use CRLF line endings.
    int
    The number of strings contained within the internal collection.
    getString​(int index)
    Returns the string at an indexed location within the internal collection.
    int
    getStringLen​(int index)
    Returns Nth string's length, in characters.
    boolean
    If true, whitespace, including carriage-returns and linefeeds, are automatically removed from the beginning and end of a string when added to the collection.
    boolean
    If true, then duplicates are not allowed.
    void
    insertAt​(int index, String str)
    Inserts a string into the internal collection at a specified index.
    Returns the last string in the internal collection.
    void
    Loads strings from a file (one per line) into the internal collection.
    void
    loadFromFile2​(String path, String charset)
    Loads strings from a file (one per line) into the internal collection.
    void
    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.
    void
    prepend​(String str)
    Adds a string to the beginning of the internal collection.
    void
    remove​(String str)
    Removes all strings equal to the string argument from the internal collection.
    void
    removeAt​(int index)
    Removes the string at a particular index.
    void
    replaceAt​(int index, String str)
    Replaces the string at a specified index.
    void
    saveNthToFile​(int index, String saveToPath)
    Saves the Nth string in the collection to a file.
    void
    saveToFile​(String path)
    Saves the collection of strings to a file, one string per line.
    void
    saveToFile2​(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.
    void
    setCrlf​(boolean value)
    Sets the Crlf property; see getCrlf().
    void
    setTrim​(boolean value)
    Sets the Trim property; see getTrim().
    void
    setUnique​(boolean value)
    Sets the Unique property; see getUnique().
    void
    sort​(boolean ascending)
    Sorts the strings in the collection in ascending or descending order.
    void
    splitAndAppend​(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.
    void
    subtract​(StringArray stringArrayObj)
    Subtracts the strings contained within stringArrayObj from the caller's internal collection.
    void
    union​(StringArray stringArrayObj)
    Performs the union set-operator.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • StringArray

      public StringArray()
      Creates a new StringArray object.
  • 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 Count property (read-only)
    • getCrlf

      public boolean getCrlf()
      If true, strings are always automatically converted to use CRLF line endings. If false, strings are automatically converted to use bare LF line endings.
      Returns:
      the value of the Crlf property
    • setCrlf

      public void setCrlf​(boolean value)
      Sets the Crlf property; see getCrlf().
      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 Length property (read-only)
    • getTrim

      public boolean getTrim()
      If true, 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 Trim property
    • setTrim

      public void setTrim​(boolean value)
      Sets the Trim property; see getTrim().
      Parameters:
      value - the new value
    • getUnique

      public boolean getUnique()
      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.
      Returns:
      the value of the Unique property
    • setUnique

      public void setUnique​(boolean value)
      Sets the Unique property; see getUnique().
      Parameters:
      value - the new value
    • append

      public void append​(String str)
      Appends a string to the end of the internal ordered collection.
      Parameters:
      str - the str argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • appendSerialized

      public 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).
      Parameters:
      encodedStr - the encodedStr argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • clear

      public void clear()
      Remove all strings from the internal collection.
    • contains

      public boolean contains​(String str)
      Returns true if the string is present in the internal collection. The string comparisons are case sensitive.
      Parameters:
      str - the str argument (not null)
      Returns:
      the result; this method never throws ChilkatException
    • find

      public 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.
      Parameters:
      findStr - the findStr argument (not null)
      startIndex - the startIndex argument
      Returns:
      the result (see ChilkatObject.getLastMethodSuccess() where the description mentions failure)
    • findFirstMatch

      public 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.

      Parameters:
      matchPattern - the matchPattern argument (not null)
      startIndex - the startIndex argument
      Returns:
      the result (see ChilkatObject.getLastMethodSuccess() where the description mentions failure)
    • getString

      public String getString​(int index)
      Returns the string at an indexed location within the internal collection. The first string is located at index 0.
      Parameters:
      index - the index argument
      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 - the index argument
      Returns:
      the result (see ChilkatObject.getLastMethodSuccess() where the description mentions failure)
    • insertAt

      public 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.
      Parameters:
      index - the index argument
      str - the str argument (not null)
    • lastString

      public String 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

      public 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.

      Parameters:
      path - the path argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • loadFromFile2

      public 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.

      Parameters:
      path - the path argument (not null)
      charset - the charset argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • loadFromText

      public 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.

      Parameters:
      str - the str argument (not null)
    • pop

      public String 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

      public void prepend​(String str)
      Adds a string to the beginning of the internal collection.
      Parameters:
      str - the str argument (not null)
    • remove

      public void remove​(String str)
      Removes all strings equal to the string argument from the internal collection.
      Parameters:
      str - the str argument (not null)
    • removeAt

      public void removeAt​(int index)
      Removes the string at a particular index.
      Parameters:
      index - the index argument
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • replaceAt

      public void replaceAt​(int index, String str)
      Replaces the string at a specified index.
      Parameters:
      index - the index argument
      str - the str argument (not null)
    • saveNthToFile

      public void saveNthToFile​(int index, String saveToPath)
      Saves the Nth string in the collection to a file.
      Parameters:
      index - the index argument
      saveToPath - the saveToPath argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • saveToFile

      public 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)
      Parameters:
      path - the path argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • saveToFile2

      public 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.
      Parameters:
      saveToPath - the saveToPath argument (not null)
      charset - the charset argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • saveToText

      public String 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

      public 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.
      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, set ascending to true, otherwise set ascending equal to false.
      Parameters:
      ascending - the ascending argument
    • splitAndAppend

      public void splitAndAppend​(String str, String boundary)
      Splits a string at a character or substring boundary and adds each resulting string to the internal collection.
      Parameters:
      str - the str argument (not null)
      boundary - the boundary argument (not null)
    • strAt

      public String strAt​(int index)
      Returns the string at a specific index.
      Parameters:
      index - the index argument
      Returns:
      the result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • subtract

      public void subtract​(StringArray stringArrayObj)
      Subtracts the strings contained within stringArrayObj from the caller's internal collection.
      Parameters:
      stringArrayObj - the stringArrayObj argument (not null)
    • union

      public 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.
      Parameters:
      stringArrayObj - the stringArrayObj argument (not null)