Delphi for .NET FCL Examples Reference
| StringCollection | namespace |
| System.Collections.Specialized |
Declaration
StringCollection = class(System.Object, IList, ICollection, IEnumerable);
Description
Represents a collection of strings
Example
The following code example demonstrates several of the properties and methods of StringCollection
program System_Collections_Specialized_StringCollection; {$APPTYPE CONSOLE} {$AUTOBOX ON} uses System.Collections, System.Collections.Specialized; type SamplesStringCollection = class public class procedure Main; private class procedure PrintValues(myCol: IEnumerable); end; { SamplesStringCollection } class procedure SamplesStringCollection.Main; type TArrayOfString = array of string; var myArr2: TArrayOfString; myArr: TArrayOfString; i: Integer; myCol: StringCollection; begin // Creates and initializes a new StringCollection myCol := StringCollection.Create; Console.WriteLine('Initial contents of the StringCollection:'); PrintValues(myCol); // Adds a range of elements from an //array to the end of the StringCollection myArr := TArrayOfString.Create ('RED', 'orange', 'yellow', 'RED', 'green', 'blue', 'RED', 'indigo', 'violet', 'RED'); myCol.AddRange(myArr); Console.WriteLine('After adding a range of elements:'); PrintValues(myCol); // Adds one element to the end of the StringCollection // and inserts another at index 3. myCol.Add('* white'); myCol.Insert(3, '* gray'); Console.WriteLine('After adding "* white" to the end and inserting "* gray" at index 3:'); PrintValues(myCol); // Removes one element from the StringCollection myCol.Remove('yellow'); Console.WriteLine('After removing "yellow":'); PrintValues(myCol); // Removes all occurrences of a // value from the StringCollection i := myCol.IndexOf('RED'); while (i > -1) do begin myCol.RemoveAt(i); i := myCol.IndexOf('RED'); end; // Verifies that all occurrences of "RED" are gone if myCol.Contains('RED') then Console.WriteLine('*** The collection still contains "RED".'); Console.WriteLine('After removing all occurrences of "RED":'); PrintValues(myCol); // Copies the collection to a new // array starting at index 0 SetLength(myArr2,myCol.Count); myCol.CopyTo(myArr2, 0); Console.WriteLine('The new array contains:'); i := 0; while (i < System.Array(myArr2).Length) do begin Console.WriteLine(' [{0}] {1}', i, myArr2[i]); i := i + 1; end; Console.WriteLine; // Clears the entire collection myCol.Clear; Console.WriteLine('After clearing the collection:'); PrintValues(myCol); end; class procedure SamplesStringCollection.PrintValues (myCol: IEnumerable); var myEnumerator: System.Collections.IEnumerator; begin myEnumerator := myCol.GetEnumerator; while myEnumerator.MoveNext do begin Console.WriteLine(' {0}', myEnumerator.Current); end; Console.WriteLine; end; //test program begin SamplesStringCollection.Main; end. |
Example output
Initial contents of the StringCollection: After adding a range of elements: RED orange yellow RED green blue RED indigo violet RED After adding "* white" to the end and inserting "* gray" at index 3: RED orange yellow * gray RED green blue RED indigo violet RED * white After removing "yellow": RED orange * gray RED green blue RED indigo violet RED * white After removing all occurrences of "RED": orange * gray green blue indigo violet * white The new array contains: [0] orange [1] * gray [2] green [3] blue [4] indigo [5] violet [6] * white After clearing the collection:
Looking for Delphi for .NET demos / source code samples? Look no further, Delphi for .NET FCL Examples Reference is what you are looking for!

