zoukankan      html  css  js  c++  java
  • Delphi中TStringList的使用

    ---------资料来源:delphi TStringList 用法详解  http://www.delphitop.com/html/wenjian/3407.html

    /TStringList 常用方法与属性 :
    var
    List: TStringList;
    i: Integer;
    begin 
    List := TStringList.Create;
    List.Add('Strings1'); {添加}
    List.Add('Strings2');
    List.Exchange(0,1); {置换}
    List.Insert(0,'Strings3'); {插入}
    i := List.IndexOf('Strings1'); {第一次出现的位置}
    List.Sort; {排序}
    List.Sorted := True; {指定排序}
    List.Count; {总数}
    List.Text; {文本集合}
    List.Delete(0); {删除, 0是第一个数据}
    List.LoadFromFile('c:/tmp.txt');{打开}
    List.SaveToFile('c:/tmp.txt'); {保存}
    List.Clear; {清空}
    List.Free; {释放}
    end;

    //读入字符串
    var
    List: TStringList;
    begin 
    List := TStringList.Create;
    List.CommaText := 'aaa,bbb,ccc,ddd';
    //相当于: List.Text := 'aaa' + #13#10 + 'bbb' + #13#10' + 'ccc' + '#13#10' + 'ddd';

    ShowMessage(IntToStr(List.Count)); //4
    ShowMessage(List[0]); //aaa

    List.Free;
    end;

    //置换分隔符
    var
    List: TStringList;
    begin 
    List := TStringList.Create;
    List.Delimiter := '|';
    List.DelimitedText := 'aaa|bbb|ccc|ddd';

    ShowMessage(IntToStr(List.Count)); //4
    ShowMessage(List[0]); //aaa

    List.Free;
    end;

    //类似的哈希表操作法
    var
    List: TStringList;
    begin 
    List := TStringList.Create;

    List.Add('aaa=111');
    List.Add('bbb=222');
    List.Add('ccc=333');
    List.Add('ddd=444');

    ShowMessage(List.Names[1]); //bbb
    ShowMessage(List.ValueFromIndex[1]); //222
    ShowMessage(List.Values['bbb']); //222

    //ValueFromIndex 可以赋值:
    List.ValueFromIndex[1] := '2';
    ShowMessage(List[1]); //bbb=2

    //可以通过 Values 赋值:
    List.Values['bbb'] := '22';
    ShowMessage(List[1]); //bbb=22

    List.Free;
    end;

    //避免重复值
    var
    List: TStringList;
    begin 
    List := TStringList.Create;

    List.Add('aaa');

    List.Sorted := True; //需要先指定排序
    List.Duplicates := dupIgnore; //如有重复值则放弃

    List.Add('aaa');

    ShowMessage(List.Text); //aaa

    //Duplicates 有3个可选值:
    //dupIgnore: 放弃;
    //dupAccept: 结束;
    //dupError: 提示错误.

    List.Free;
    end;


    //排序与倒排序
    {排序函数}
    function DescCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;
    begin 
    Result := -AnsiCompareText(List[Index1], List[Index2]);
    end;

    procedure TForm 1.Button1Click(Sender: TObject);
    var
    List: TStringList;
    begin 
    List := TStringList.Create;

    List.Add('bbb');
    List.Add('ccc');
    List.Add('aaa');

    //未排序
    ShowMessage(List.Text); //bbb ccc aaa

    //排序
    List.Sort;
    ShowMessage(List.Text); //aaa bbb ccc

    //倒排序
    List.CustomSort(DescCompareStrings); //调用排序函数
    ShowMessage(List.Text); //ccc bbb aaa

    //假如:
    List.Sorted := True;
    List.Add('999');
    List.Add('000');
    List.Add('zzz');
    ShowMessage(List.Text); //000 999 aaa bbb ccc zzz
    end;

    --------------------------资料结束

    ----------------------unit开始

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls ;

    type
    TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;
    sList:TStringList ;
    implementation

    {$R *.dfm}

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    sList:=TStringList.Create;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    sList.Clear;
    sList.Add('A=AAA');
    sList.Add('B=BBB');
    sList.Add('C=CCC');
    sList.Add('D=DDD');
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    var
    i:Integer;
    begin
    Memo1.Clear;
    for i:=0 to sList.Count -1 do
    begin
    Memo1.Lines.Add('Name: '+sList.Names[i]+' Value: '+sList.ValueFromIndex[i] );
    Memo1.Lines.Add('Values: '+sList.Values[sList.Names[i]] );
    end;

    end;

    procedure TForm1.Button3Click(Sender: TObject);
    begin
    sList.Delimiter:=';';
    sList.DelimitedText:='A=AAA;B=BBB;C=CCC;D=DDD';
    end;

    procedure TForm1.Button4Click(Sender: TObject);
    begin
    sList.CommaText := 'A=AAA,B=BBB,C=CCC,D=DDD';
    //sList.text := 'A=AAA' + #13#10 + 'B=BBB' + #13#10 + 'C=CCC'+#13#10 + 'D=DDD';
    end;

    end.

    ---------------------unit结束------------------------------------------

    ------------form开始

    object Form1: TForm1
    Left = 454
    Top = 194
    Width = 351
    Height = 349
    Caption = 'Form1'
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    OldCreateOrder = False
    OnCreate = FormCreate
    PixelsPerInch = 96
    TextHeight = 13
    object Button1: TButton
    Left = 8
    Top = 16
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
    end
    object Button2: TButton
    Left = 224
    Top = 144
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 1
    OnClick = Button2Click
    end
    object Memo1: TMemo
    Left = 8
    Top = 56
    Width = 185
    Height = 241
    ImeName = '中文(简体) - 搜狗拼音输入法'
    Lines.Strings = (
    'Memo1')
    ScrollBars = ssBoth
    TabOrder = 2
    end
    object Button3: TButton
    Left = 96
    Top = 16
    Width = 75
    Height = 25
    Caption = 'Button3'
    TabOrder = 3
    OnClick = Button3Click
    end
    object Button4: TButton
    Left = 184
    Top = 16
    Width = 75
    Height = 25
    Caption = 'Button4'
    TabOrder = 4
    OnClick = Button4Click
    end
    end

    -------------------form结束

  • 相关阅读:
    操作系统设计与实现(二)
    SpringCloud(八)Consul的微服务注册
    图的实现(邻接矩阵)及DFS、BFS
    SpringCloud(七)服务注册之Consul的简介和原理
    Mybatis笔记目录(6天)
    Mybatis学习笔记——day02
    C语言教程Day01
    Linux C/C++方向开发(13周学习路线)
    基于Java的实验室预约管理系统
    基于Android的高校学生考勤系统的设计与实现
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/14764483.html
Copyright © 2011-2022 走看看