Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
The Chilkat StringArray objectThe Chilkat string array object is a utility class used by many of the Chilkat components. It's not an array. It's an object that contains 0 or more strings that can be retrieved by index. The reason Chilkat created this object is that string arrays are handled differently in different programming languages. The CkStringArray object (or Chilkat.StringArray in .NET) is used whenever a collection of strings is passed to a method, or returned from a method. This example demonstrates some very basic usage. How to create a string array, populate it with some entries, sort it, retrieve entries, etc.
// This is not an array -- it's an object. Chilkat.StringArray sa = new Chilkat.StringArray(); // Append some strings. sa.Append("abc"); sa.Append("Abc"); sa.Append("123"); sa.Append("Chilkat Software, Inc."); // How many strings? int n; n = sa.Count; textBox1.Text += Convert.ToString(n) + "\r\n"; textBox1.Refresh(); // Iterate over the strings contained in the object: int i; for (i = 0; i <= n - 1; i++) { textBox1.Text += sa.GetString(i) + "\r\n"; textBox1.Refresh(); } // Sort in ascending order: bool ascending; ascending = true; sa.Sort(ascending); textBox1.Text += "Sorted:" + "\r\n"; textBox1.Refresh(); for (i = 0; i <= n - 1; i++) { textBox1.Text += sa.GetString(i) + "\r\n"; textBox1.Refresh(); } // Save to a file (one string per line). // Line endings are controlled by the Crlf property. bool useCrlf; useCrlf = true; sa.Crlf = useCrlf; sa.SaveToFile("strings.txt"); // Clear the string array. sa.Clear(); // Load the string array from a file. Each line in the // file becomes a string contained in the object: sa.LoadFromFile("strings.txt"); // Remove a string by exact match: sa.Remove("abc"); // Remove a string by index (1st string is at index 0): sa.RemoveAt(2); // The Trim property can be set to automatically trim whitespace // from the beginning and end of any string added to the object: sa.Trim = true; // Append some strings from a comma-separated list: sa.SplitAndAppend("apple, orange, banana, pear, lime",","); // What do we have now? textBox1.Text += "-------- After SplitAndAppend:" + "\r\n"; textBox1.Refresh(); n = sa.Count; for (i = 0; i <= n - 1; i++) { textBox1.Text += sa.GetString(i) + "\r\n"; textBox1.Refresh(); } // Serialize the complete object to a base64 string: string serialized; serialized = sa.Serialize(); textBox1.Text += "-------- Serialized:" + "\r\n"; textBox1.Refresh(); textBox1.Text += serialized + "\r\n"; textBox1.Refresh(); // Restore from a serialized string: Chilkat.StringArray sa2 = new Chilkat.StringArray(); sa2.AppendSerialized(serialized); textBox1.Text += "-------- After AppendSerialized:" + "\r\n"; textBox1.Refresh(); n = sa2.Count; for (i = 0; i <= n - 1; i++) { textBox1.Text += sa2.GetString(i) + "\r\n"; textBox1.Refresh(); } // Set the Unique property to prevent duplicates from being added: sa.Unique = true; sa.Clear(); sa.Append("apple"); sa.Append("banana"); sa.Append("apple"); sa.Append("banana"); // What do we have now? textBox1.Text += "-------- Unique strings:" + "\r\n"; textBox1.Refresh(); n = sa.Count; for (i = 0; i <= n - 1; i++) { textBox1.Text += sa.GetString(i) + "\r\n"; textBox1.Refresh(); } // Find the location of a specific string // (case insensitive). int index; int startIndex; startIndex = 0; index = sa.Find("apple",startIndex); if (index >= 0) { textBox1.Text += "found apple at index " + Convert.ToString(index) + "\r\n"; textBox1.Refresh(); } else { textBox1.Text += "apple not found" + "\r\n"; textBox1.Refresh(); } |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.