• TStringList常用操作


    复制代码
    复制代码
    //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:	mp.txt');{打开}
      List.SaveToFile('c:	mp.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');
      List.Add('eee=');
      List.Add('fff');
    
      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
    
      ShowMessage(List.Names[4]);  //eee
      ShowMessage(List.ValueFromIndex[4]);  //
      ShowMessage(List.Values['eee']);  //
    
      ShowMessage(List.Names[5]);  //
      ShowMessage(List.ValueFromIndex[5]);  //
      ShowMessage(List.Values['fff']);  //
    
      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 TForm1.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;
  • 相关阅读:
    最简洁的Mysql修改密码,不会你打我
    两步解决jetbrains-agent-latest.zip不能拖入工作区
    JSON学习笔记
    【书评】【不推荐】《机器学习实战——基于Scikit-Learn和TensorFlow》
    weblogic更新部署
    功能网址
    jupyter快捷键
    MYSQL连接字符串参数说明
    C# 格式验证
    Supervisor
  • 原文地址:https://www.cnblogs.com/bjxsky/p/4619863.html
Copyright © 2020-2023  润新知