' add and insert items
Sub StringList_AddItem(ByRef $list As String, $item As String)
Sub StringList_InsertItem(ByRef $list As String, $index As Integer, $item As String)

' change item with specified index
Sub StringList_ChangeItem(ByRef $list As String, $index As Integer, $item As String)

' delete item with specified index
Sub StringList_DeleteItem(ByRef $list As String, $index As Integer)

' get item with specified index
Sub StringList_GetItem($list As String, $index As Integer) As String

' get item index (or test if item exists)
Sub StringList_GetItemIndex($list As String, $item As String) As Integer

' get item count
Sub StringList_ItemCount($list As String) As Integer

' sort all strings in the list
Sub StringList_Sort(ByRef $list As String)

' removes duplicates from a string list
Sub StringList_RemoveDuplicates(ByRef $list As String)

'removes empty lines/items from a string list
Sub StringList_RemoveEmptyItems(ByRef $list As String)


' Load a text file and store each line as item in a string list
Sub StringList_LoadFromFile($filename As String, ByRef $list As String) As Boolean

' Save a string list to a file
Sub StringList_SaveToFile($filename As String, $list As String) As Boolean


A stringlist is a simple string in which each list item is stored on a separate line.


Lines are separated by a carriage return and line feed (chr(13)+chr(10), or CRLF).


The stringlist functions can be used to maintain such a list easily, for example to insert or delete individual items.


You can also add strings that contain line breaks to a stringlist. All carriage return and line feed characters are encoded automatically within the stringlist.


Stringlists are zero based, which means that the first item has index 0, the second item has index 1, and so on. The number of items can be determined with the function "StringList_ItemCount".


The function "StringList_GetItemIndex" returns the index of a specific item. If the item is not found, the function returns -1.


Example:

Dim $sl, $s, $i, $sItem, $nCount

' Clear the string list
$sl = ""

' Add items to the stringlist
StringList_AddItem($sl, "WebSite")
StringList_AddItem($sl, "Watcher")
StringList_AddItem($sl, "cool")
' items are now: WebSite, Watcher, cool

' Insert item after "Watcher"
StringList_InsertItem($sl, 2, "is")
' items are now: WebSite, Watcher, is, cool

' Check if item exists
$i = StringList_GetItemIndex($sl, "power")
' returns $i = -1

$i = StringList_GetItemIndex($sl, "is")
' returns $i = 2

' Change last item
StringList_ChangeItem($sl, 3, "very cool")
' items are now: WebSite, Watcher, is, very cool

' Iterate through all items
$s = ""
$nCount = StringList_ItemCount($sl)
For $i = 0 To $nCount - 1
   $s = Trim($s + " " + StringList_GetItem($sl, $i))
Next
' $s is now: WebSite-Watcher is very cool

' Sort the string list
StringList_Sort($sl)
' items are now: is, very cool, Watcher, WebSite

' Delete item with index 2
StringList_DeleteItem($sl, 2)
' items are now: is, very cool, WebSite

' Delete item "WebSite"
StringList_DeleteItem($sl, StringList_GetItemIndex($sl, "WebSite"))
' items are now: is, very cool

' Save string list to file
StringList_SaveToFile("c:\files\stringlist.txt", $sl)