RAD Studio VCL Reference
|
References the strings in the list by their positions.
property Strings [Index: Integer]: string;
__property AnsiString Strings[int Index];
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 or set the string at a particular position.
To store a string as a name-value pair, assign Strings a value that includes the NameValueSeparator character. The name and value will then be accessible separately using the Names and Values properties.
MyStrings[0] := 'This is the first string';
C++ Examples:
/* 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. Choose multiple files using the CNTL key or the SHIFT key. */ #include <stdio.h> // for FILE void __fastcall TForm1::Button1Click(TObject *Sender) { FILE *stream; char FirstLine[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(AnsiString(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); } } } }
/* The following 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. TListBox objects that are not name-value pairs are not assigned a Name and IndexOfName will return -1. TStrings Values are referenced using the name AnsiString as an index. */ void MergeStrings(TStrings *Dest, TStrings *Source) { for (int i = 0; i < Source->Count; i++) { if (Source->Strings[i].Pos("=") > 1) { int DI = Dest->IndexOfName(Source->Names[i]); if (DI > -1) Dest->Values[Source->Names[i]] = Source->Values[Source->Names[i]]; } } } void __fastcall TForm1::Button1Click(TObject *Sender) { MergeStrings(ListBox1->Items,ListBox2->Items); } void __fastcall TForm1::FormCreate(TObject *Sender) { ListBox1->Items->Add("Plants = 10"); ListBox1->Items->Add("Animals = 20"); ListBox1->Items->Add("Minerals = 15"); ListBox2->Items->Add("Animals = 4"); ListBox2->Items->Add("Plants = 3"); ListBox2->Items->Add("Minerals = 78"); }
/* This example uses a button and a memo control on a form. When the user clicks the button, the content of the specified file is loaded into the memo, and the fifth line of the file is written across the top of the form. */ void __fastcall TForm1::Button1Click(TObject *Sender) { Memo1->Lines->LoadFromFile("../readme.txt"); Caption = Memo1->Lines->Strings[4]; }
Delphi Examples:
{ 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. Choose multiple files using the CNTL key or the SHIFT key. } procedure TForm1.Button1Click(Sender: TObject); var I: integer; F: TextFile; FirstLine: string; begin OpenDialog1.Options := [ofAllowMultiSelect, ofFileMustExist]; OpenDialog1.Filter := 'Text files (*.txt)|*.txt|All files (*.*)|*.*'; OpenDialog1.FilterIndex := 2; { start the dialog showing all files } if OpenDialog1.Execute then with OpenDialog1.Files do for I := 0 to Count - 1 do begin AssignFile(F, Strings[I]); { next file in Files property } Reset(F); Readln(F, FirstLine); { Read the first line out of the file } Memo1.Lines.Append(FirstLine); { Add the line to the memo } CloseFile(F); end; end;
{ The following 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. TListBox objects that are not name-value pairs are not assigned a Name and IndexOfName will return -1. } procedure MergeStrings(Dest, Source: TStrings); var I, DI: Integer; begin for I := 0 to Source.Count - 1 do begin if Pos ('=', Source[I]) > 1 then begin DI := Dest.IndexOfName(Source.Names[I]); if DI > -1 then Dest[DI] := Source[I]; end; end; end; procedure TForm1.Button1Click(Sender: TObject); begin MergeStrings(ListBox1.Items, ListBox2.Items); end; procedure TForm1.FormCreate(Sender: TObject); begin ListBox1.Items.Add('Plants = 10'); ListBox1.Items.Add('Animals = 20'); ListBox1.Items.Add('Minerals = 15'); ListBox2.Items.Add('Animals = 4'); ListBox2.Items.Add('Plants = 3'); ListBox2.Items.Add('Minerals = 78'); end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|