delphi TStringList 用法详解
转自: http://blog.163.com/you888@188/blog/static/67239619201472365642633/
delphi TStringList 用法详解
2014-08-23 18:56:42| 分类: delphi xe6以上|举报|字号 订阅
2014-5-2阅读63 评论0
delphi 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:/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;
delphi TStringList 用法详解的更多相关文章
- Delphi TStringHelper用法详解
Delphi TStringHelper用法详解 (2013-08-27 22:45:42) 转载▼ 标签: delphi_xe5 it 分类: Delphi Delphi XE4的TStringHe ...
- Delphi IDHTTP用法详解(六种用法)
一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入ID ...
- Delphi IDHTTP用法详解
一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入 ...
- 教程-Delphi中Spcomm使用属性及用法详解
Delphi中Spcomm使用属性及用法详解 Delphi是一种具有 功能强大.简便易用和代码执行速度快等优点的可视化快速应用开发工具,它在构架企业信息系统方面发挥着越来越重要的作用,许多程序员愿意选 ...
- delphi中Application.MessageBox函数用法详解
delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...
- Delphi XE4 TStringHelper用法详解
原文地址:Delphi XE4 TStringHelper用法详解作者:天下为公 Delphi XE4的TStringHelper,对操作字符串进一步带来更多的方法,估计XE5还能继续用到. Syst ...
- Delphi Format函数功能及用法详解
DELPHI中Format函数功能及用法详解 DELPHI中Format函数功能及用法详解function Format(const Format: string; const Args: array ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- @RequestMapping 用法详解之地址映射
@RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...
随机推荐
- too few PGs per OSD (20 < min 30)
ceph osd pool set replicapool pg_num 150 ceph osd pool set replicapool pgp_num 150
- centos7 /etc/profile /etc/bashrc
在/etc/profile中添加环境变量后,是使用source /etc/profile编译后只能在当前终端生效 重新开启一个终端后,该环境变量失效. 解决方法: 重启系统:reboot,问题解决 环 ...
- 网页信息抓取 Jsoup的不足之处 httpunit
今天又遇到一个网页数据抓取的任务,给大家分享下. 说道网页信息抓取,相信Jsoup基本是首选的工具,完全的类JQuery操作,让人感觉很舒服.但是,今天我们就要说一说Jsoup的不足. 1.首先我们新 ...
- Resume (Curriculum Vitae)
The resume (Curriculum Vitae) is a selling tool outlining your skills and experience so an employer ...
- Distributing Ballot Boxes
Distributing Ballot Boxes http://acm.hdu.edu.cn/showproblem.php?pid=4190 Time Limit: 20000/10000 MS ...
- The Closest M Points
The Closest M Points http://acm.hdu.edu.cn/showproblem.php?pid=4347 参考博客:https://blog.csdn.net/acdre ...
- centos 6.5 ftp服务配置及客户端使用
一.ftp服务简介 FTP是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”.用于Internet上的控制文件的双向传输.同时,它也是一个应用程序(Ap ...
- 本地Maven环境配置
本地Maven环境配置 下载配置文件:http://10.1.10.138:6060/root/DevelopmentSpecification/archive/master.zip 解压master ...
- Spring框架的属性注入
1. 对于类成员变量,常用的注入方式有两种 * 构造函数注入(没有空的构造方法注入) * 属性setter方法注入(有空的构造方法注入) 2. 在Spring框架中提供了前两种的属性注入的方式 1. ...
- 一句话引发的思考 - synchronized/super
https://blog.csdn.net/cool__wang/article/details/52459380#commentBox