The Delphi application often handles string lists, such as strings in combo boxes and list boxes, lines of text for TMemo parts, lists of fonts supported by screens, tabs properties of Tnotebook parts, rows and columns of string grids, and so on.
Although applications use these lists in different ways, Delphi provides a unified interface through an object called a string list (Tstrings), and can be converted to each other on different occasions. For example, you can edit a string in the TMemo part and use it as a list item in a list box.
A list of strings is often used in the Delphi integrated development environment. If the tstrings character is often listed in the Value column of the Object Inspector form, double-clicking the character will eject the character list Editor, as shown in Figure 3.1, which can be edited, added, and deleted in the editor.
A list of strings can also be manipulated while running, and a common string list operation is as follows:
Action string in list
Load, save a list of strings
Create a list of strings
To add an object to a list of strings
This chapter describes the common operations and simple applications of string lists.
3.1 Common operations for a list of strings
3.1.1 Action string in list
In the Delphi application, the strings in the list are often manipulated. For example, you modify the string list property at design time.
The common string operations are as follows:
Count the number of strings in a list
Accessing the specified string
Find the location of the string
Add a string to the list
Delete a string from a list
Move a string in a list
Copy a complete list of strings
To copy a string from a list
3.1.1.1 count the number of strings in the list
Use the Count property to calculate the number of strings in the list. Count is a read-only property that indicates the number of string lists in the list. Because the list of strings is indexed with zero, the count is greater than the maximum index of the list.
For example, the application wants to calculate the number of fonts supported on the current screen, and you can find a list of fonts for screen objects that contain the names of all the fonts supported by the screen.
Fontcount:=screen.fonts.count;
3.1.1.2 access to the specified string
The list of strings has an indexed strings property that can be used like a string array. strings For example, the first string in the list is strings[0]. Because the strings property is the most commonly used property in the string list, the strings property can be used as the default property of the string list, that is, the strings identifier can be omitted when you use it.
To access the specified character in the string, find the starting position or index of the character. The number of strings is counted at zero. If there are three strings in the list, the index range is 0. 2.
The following code is equivalent:
Memol.lines.strings[0]:= ' This is the ' the '.
Memol.lines[0]:= ' This is the ' the '.
3.1.1.3 find the location of the string
The IndexOf method finds the location of the specified string. IndexOf has a parameter of type string that returns the position of the matching string in the list. If there is no matching string in the list, returns-1.
The IndexOf method can only find the full string, that is, the entire string must be completely matched. If you match only part of the string, you must write the appropriate code.
The following code determines whether a specified string is in the list:
If FileListBox1.Items.IndexOf (' AUTOEXEC. BAT ') >-1 Then
Begin
Color: = Clyellow;
Label1.Caption: = ' You are in the root directory! ';
End
3.1.1.4 Add a string to the list
There are two ways to add a string to the list: You can add a string to the end of the list, or you can insert it into the list.
To add a string to the tail of the list, use the Add method to pass the string as a parameter.
To insert a string into the list, use the Insert method to pass two arguments: the insertion position and the string.
For example, to insert "Three" into the third position in the list, use code insert (2, ' Three '). If there are less than 2 characters in the list, Delphi produces an exception that exceeds the index range (see Chapter 12 for exceptions).
3.1.1.5 Move strings in list
The application can move the specified string to another location in the list, and if the string is connected to an object, the object is synchronized with the string.
The Move method enables the movement of the string, which has two parameters: the current position and the position to move. The following code moves the third string to the fifth position:
Move (2,4);
3.1.1.6 Delete a string from a list
Use the Delete method to delete the specified string. The parameter to delete is the position of the specified string, and if you do not know the position of the string, use the IndexOf method.
To delete all strings in the string list, use the Clear method.
The following code deletes the specified string in the list box:
With Listbox1.items do
Begin
If Indexof (' bureaucracy ') >-1 then
Delete (Indexof (' bureaucracy '));
End
3.1.1.7 copy complete list of strings
Copying a list to another list is equivalent to assigning the source list to the target list, even if the list is subordinate to a different part, and Delphi can replicate that.
The copy list overrides the target list and uses the AddStrings method if you want to add the source list to the end of the target list.
The following code is a copy list and a list of connections, respectively:
Outline1.lines:=combobox1.items;
Outline1.addstrings (Combobox1.items);
3.1.1.8 a string in a repeat action list
Many situations require manipulation of each string in a table, such as changing the case of a string. Such repetitive actions can be implemented with a for loop, while using an index of the integer type of the list.
The following code repeats the string of the list box. When the user presses the button, the string in the list box is converted to uppercase and lowercase.
Procedure Tform1.button1click (Sender:tobject);
Var
I:integer;
Begin
For I: = 0 to Listbox1.items.count-1 do
Listbox1.items[i]: = Uppercase (Listbox1.items[i]);
End