Example code : A simple example
------------------------------------------
var
animals : TStringList; // Define our string list variable
i : Integer;
begin
// Define a string list object, and point our variable at it
animals := TStringList.Create;
// Now add some names to our list
animals.Add('Cat');
animals.Add('Mouse');
animals.Add('Giraffe');
// Now display these animals
for i := 0 to animals.Count-1 do
ShowMessage(animals[i]); // animals[i] equates to animals.Strings[i]
// Free up the list object
animals.Free;
end;
Show full unit code
Cat
Mouse
Giraffe
--------------------------------------------------
Example code : Using name-value strings
---------------------------------------------------
var
names : TStringList; // Define our string list variable
ageStr : String;
i : Integer;
begin
// Define a string list object, and point our variable at it
names := TStringList.Create;
// Now add some names to our list
names.CommaText := 'Neil=45, Brian=63, Jim=22';
// And now find Brian's age
ageStr := names.Values['Brian'];
// Display this value
ShowMessage('Brians age = '+ageStr);
// Now display all name and age pair values
for i := 0 to names.Count-1 do
begin
ShowMessage(names.Names[i]+' is '+names.ValueFromIndex[i]);
end;
// Free up the list object
names.Free;
end;
Show full unit code
Brians age is 63
Neil is 45
Brian is 63
Jim is 22
-----------------------------------------------------------------------------
Example code : Using DelimitedText, Delimiter and QuoteChar
-----------------------------------------------------------------------------
var
cars : TStringList; // Define our string list variable
i : Integer;
begin
// Define a string list object, and point our variable at it
cars := TStringList.Create;
// Now add some cars to our list - using the DelimitedText property
// with overriden control variables
cars.Delimiter := ' '; // Each list item will be blank separated
cars.QuoteChar := '|'; // And each item will be quoted with |'s
cars.DelimitedText := '|Honda Jazz| |Ford Mondeo| |Jaguar "E-type"|';
// Now display these cars
for i := 0 to cars.Count-1 do
ShowMessage(cars[i]); // cars[i] equates to cars.Strings[i]
// Free up the list object
cars.Free;
end;
轉貼至 http://www.delphibasics.co.uk/RTL.asp?Name=TStringList
-----------------------------------------------------------------------------
排除重複的資料內容
-----------------------------------------------------------------------------
排除重複的資料內容
-----------------------------------------------------------------------------
var sList:TStringList;
begin
sList := TStringList.Create;
sList.Sorted := True;
sList.Duplicates := dupIgnore; //dupIgnore, dupAccept, dupError
sList.Add('test');
sList.Add('test');
end
沒有留言:
張貼留言