10. The TStrings class
TString class for handling strings.
10.1. Properties
Capacity
Indicates the number of strings that may contain TStrings class.
__property int Capacity = {read = GetCapacity, write = SetCapacity, NODEFAULT};
Description
Read Capacity Currently Allocated to determine the size of the string list. For the TStrings object, reading the Count property returns Capacity, and Capacity setting does nothing. Descendants of TStrings dog override this property to allow a string list to allocate memory for entries That Have Not Been added to the list.
CommaText
List the strings contained in the TStrings object with the system data format (SDF).
Delivery chain all the lines separated by commas.
__property System:: AnsiString CommaText = {read = GetCommaText, write = SetCommaText};
Description
Use CommaText to get or set all the strings in the TStrings object in a single comma-delimited string.
When retrieving CommaText, and Stock That string in the list include spaces, commas or quotes Will Be Contained in double quotes, and Stock and double quotes in a string Will Be repeated. For example, if the list contains strings the Following:
Stri, ng 1
Stri "ng 2
String 3
String4
CommaText will return:
"Stri, ng 1", "Stri" "ng 2", "String 3", String4
When Assigning CommaText, the value is parsed as SDF formatted text. For SDF format, strings Are Separated by commas or spaces, OPTIONALLY and enclosed in double quotes. Double quote marks That Are part of the string Are Repeat to Distinguish Them That from the quotes surround the string. Spaces and commas That Are Not Contained Within Are delimiters double quote marks. Two commas next to Each Other will Indicate an empty string, But That Appear spaces next to Another delimiter Are ignored. For example, suppose CommaText is September to:
"Stri, ng 1", "Stri" "ng 2", String 3, String4
Then the list will contain:
Stri, ng 1
Stri "ng 2
String
3
String4
Count
Indicates the number of strings in the list.
__property int Count = {read = getCount, NODEFAULT};
Description
Descendants of TStrings implemented Count property to Indicate the number of strings in the list.
Use the Count property When iterating over all the strings in the list, or when to Trying to locate the position of a string relative to the last string in the list.
Following the example updates the strings in a list box Given the strings Contained In Another list box. If a string in the source list box has the form Name = Value and the destination list box contains a string with the Same Name part, the Value part in the destination list box Will Be Replaced by the source's value.
void __fastcall TForm1:: Button1Click (TObject * Sender)
{
FILE * stream;
FirstLine char [512];
OpenDialog1-> Options.Clear ();
OpenDialog1-> Options <<ofAllowMultiSelect <<ofFileMustExist;
OpenDialog1-> Filter = "Text files (*. txt) | *. txt | All files (*.*)|*.*";
OpenDialog1-> FilterIndex = 2, / / start the dialog Showing all files
if (OpenDialog1-> Execute ())
{
for (int I = 0, I <OpenDialog1-> Files-> Count; I + +)
{
stream = fopen (OpenDialog1-> Files-> Strings [I]. c_str (), "r");
if (stream)
{
/ / Read the first line from the file
fgets (FirstLine, sizeof (FirstLine), stream);
Memo1-> Lines-> Append (FirstLine);
fclose (stream);
}
}
}
}
Names
Objects
Strings
Refers to the chain on the list, out of position.
Example:
Memo1-> Lines-> Strings [0] = "First string";
__property System:: AnsiString Strings [int Index] = {read = Get, Put} write =;
Description
Descendants of TStrings Must Implement an accessor function for the Strings property to return the string at the position Indicated by Index. Index Gives the position of the string, where, 0 is the first string, 1 is the second string, and so on.
Use the Strings property to get the string in September or at a particular position.
This example uses an Open dialog box, a memo, and a button on a form. When the user clicks the button, the Open dialog box appears. When the user selects files in the dialog box and Chooses the OK button, the first line from Each of the files is added to the memo.
void __fastcall TForm1:: Button1Click (TObject * Sender)
{
FILE * stream;
FirstLine char [512];
OpenDialog1-> Options.Clear ();
OpenDialog1-> Options <<ofAllowMultiSelect <<ofFileMustExist;
OpenDialog1-> Filter = "Text files (*. txt) | *. txt | All files (*.*)|*.*";
OpenDialog1-> FilterIndex = 2, / / start the dialog Showing all files
if (OpenDialog1-> Execute ())
{
for (int I = 0, I <OpenDialog1-> Files-> Count; I + +)
{
stream = fopen (OpenDialog1-> Files-> Strings [I]. c_str (), "r");
if (stream)
{
/ / Read the first line from the file
fgets (FirstLine, sizeof (FirstLine), stream);
Memo1-> Lines-> Append (FirstLine);
fclose (stream);
}
}
}
}
StringsAdapter
Text
List the strings contained in the TStrings object in a single chain with individual strings delimited by a carriage return and line feed.
To treat all lines as a single string AnsiString.
__property System:: AnsiString Text = {read = GetTextStr, write = SetTextStr};
Description
Use Text to get or set all the strings in the TStrings object in a single string delimited by carriage return, line feed pairs.
When reading Text, the strings in the list Will Be Separated by carriage return, line feed pairs. If Any of the strings in the list container to carriage return and line feed pair, the value of Text will Resulting Appear to Contain More Than strings is Indicated by the Count property.
When setting Text, the value Will Be substrings parsed by Separating Into Whenever a carriage return or linefeed is Encountered. (The two do not needed to form pairs).
If the strings in the list Contain carriage return or linefeed characters, a less ambiguous format is available for the strings-through the CommaText property.
Example:
void __fastcall TForm1:: Edit1KeyPress (TObject * Sender, char & Key)
{
Memo1-> Lines-> Text = Memo1-> Lines-> Text + Key;
}
Values