[Inno Setup] 字符串列表,当要处理一长串文件时很有用
https://wiki.freepascal.org/TStringList-TStrings_Tutorial
TStringList-TStrings Tutorial
│ Deutsch (de) │ English (en) │ español (es) │ suomi (fi) │ français (fr) │ русский (ru) │
Contents
TStringList
The TStringList (or its parent TStrings)
is much like a fancy dynamic array or Set of Strings (a set of strings
is not possible in FPC). It will come in handy a lot when programming
and I'm going to teach you basic TStringList usage!
Simple example
program StrList;
{$mode objfpc}
uses
Classes, SysUtils;
var
Str: TStringList;
begin
Str := TStringList.Create; // This is needed when using this class(or most classes)
Str.Add('Some String!');
writeln('The stringlist now has ' + IntToStr(Str.Count) + ' string(s).');
Readln;
Str.Free; //Release the memory used by this stringlist instance
end.
This is a simple console program that will create and add one string to a stringlist. Now here's some things you should know:
Create - Will create the string list for modifying. If you use Create, you have to later Free it and release the memory it takes. If not, you program will not crash, but it will not release all the memory it occupied: a memory leak.
Count - This property is a counter for the number of strings in the List.
Add - This method allows you to add one string to the stringlist. It is a function that will return the Index of the String. This is where the counter comes in handy.
Delete - Will delete a string from the stringlist. Just know that you do not simply input the string, you have to input the index of the string. Like I said: it's like a fancy Dynamic Array.
IndexOf - Will return the index of the string in the list. If it is not found it returns -1.
Clear - Will clear the list.
Expanded Example
How about a more juicy example, eh?
program StrList2;
{$mode ObjFPC}
uses
Classes, SysUtils; var
Str: TStringList;
S: String;
Counter: Integer;
begin
Str := TStringList.Create;
Writeln('String List Test');
repeat
Writeln('Enter a string to add (type EXIT to stop adding strings)');
Readln(S);
if (S = 'EXIT') then
Break; // exit the loop if (S <> '') then
begin
Counter := Str.Add(S);
Writeln('String: ' + S + ' was Added!');
Writeln('Index is: ' + IntToStr(Counter)); // The counter will always become the index of the last thing added
end
else
begin
Writeln('No data entered...');
end;
until (S = 'EXIT');
writeln('Contents of the TStringList: '+ Str.CommaText);
Str.Free; //release the memory again
end.
However, to avoid possible memory leaks you should always use a Try - Finally block where possible for this, so you get something like:
var
slist: TStringList; ... slist := TStringList.Create;
try
...
// do things with your stringlist
...
finally
if Assigned(slist) then
FreeAndNil(slist);
end;
// This works perfect, no double creation of stringlist... comments free to send to edgarrod71@gmail.com
function theStringList: TStringList;
var
J: integer;
begin
result := TStringList.Create;
for J:=0 to 10 do
result.add(intToStr(J));
end; procedure Caller;
var
SL: TStringList;
K: integer;
begin
SL := theStringList;
for K:=0 to pred(SL.Count) do
writeln(SL[K]);
if assigned(SL) then
SL.Free;
end;
Conversion to and from delimited strings
Code below will result in a stringlist, containing 4 elements ('1', '2', '3' and '4');
procedure Sample;
var
MyStringList: TStringList=nil;
begin
MyStringList:= TStringList.create;
MyStringList.Delimiter := ';';
MyStringList.DelimitedText:='1;2;3;4';
MyStringList.free;
end;
Respectively next code will assemble a stringlist into a delimited string ('1;2;3;4'):
function Sample2 : string;
var
MyStringList: TStringList=nil;
begin
MyStringList:= TStringList.create;
MyStringList.Delimiter := ';';
MyStringList.Add('1');
MyStringList.Add('2');
MyStringList.Add('3');
MyStringList.Add('4');
Result :=MyStringList.DelimitedText;
MyStringList.free;
end;
Note that Delimter is a character, not a string!
If your separator is a string (for example „\n‟), you could use code
below, to get a stringlist, containing 4 elements ('1', '2', '3' and
'4'):
procedure Sample;
var
MyStringList: TStringList=nil;
begin
MyStringList:= TStringList.create;
MyStringList.text:=StringReplace('1\n2\n3\n4','\n',Lineending,[rfReplaceAll, rfIgnoreCase]);
MyStringList.free;
end;
Vice versa, next function will return „1\n2\n3‟:
Function Sample : string;
var
MyStringList: TStringList=nil;
begin
MyStringList:= TStringList.create;
MyStringList.SkipLastLineBreak := True;
MyStringList.add('1');
MyStringList.add('2');
MyStringList.add('3');
result := StringReplace(MyStringList.Text,Lineending,'\n', [rfReplaceAll, rfIgnoreCase]);
MyStringList.free;
end;
File Handling
When using the TStringList you have 2 file handling procedures: SaveToFile and LoadFromFile. SavetoFile will save all strings in the list to a file. LoadFromFile will open the file and add the file data to the list string by string.
program StrListFile;
{$mode objfpc}
uses
Classes, SysUtils; var
Str: TStringList;
begin
Str := TStringList.Create;
try
Str.LoadFromFile('SomeFile.txt');
Str.Add('Hello');
Str.SaveToFile('SomeFile.txt');
finally
Str.Free;
end;
end.
You just opened a file, edited it and saved it right back to were it was!
Dynamic string array comparison
TStringList is simply an object-oriented version of a dynamic string array. Some methods have analogs:
| Operation | array of string | TStringList |
|---|---|---|
| Variable declaration | StringList: array of string; | StringList: TStringList; |
| Initialization | implicit constructor | StringList := TStringList.Create |
| Set size | SetLength(StringList, X); | StringList.Size := X; |
| Get size | X := Length(StringList); | X := StringList.Count; |
| Add item | SetLength(StringList, Length(StringList) + 1); StringList[Length(StringList) - 1] := X; | StringList.Add(X); |
| Delete item | for I := Index to Length(StringList) - 2 do StringList[I] := StringList[I + 1]; SetLength(StringList, Length(StringList) - 1); | StringList.Delete(Index); |
| Remove all items | SetLength(StringList, 0); | StringList.Clear; |
| Finalization | implicit destructor | StringList.Free; |
However, TStringList offers much more functionality than a basic structure such as a dynamic array.
Keep Learning
TStringList has many other interesting features:
- It allows you to sort the strings
- It allows you to limit the list to only unique strings
- You can get the text of all strings as a single string using the Text property.
- You can store an object or other data next to the string
You can learn all the different procedures, functions and properties. See TStringList documentation... or the help in Lazarus.
... and you might like to extend this tutorial if you feel like it.
See also
[Inno Setup] 字符串列表,当要处理一长串文件时很有用的更多相关文章
- Inno Setup入门(八)——有选择性的安装文件
这主要使用[Components]段实现,一个演示的代码如下: [setup] ;全局设置,本段必须 AppName=Test AppVerName=TEST DefaultDirName=" ...
- Inno Setup入门(八)——有选择性的安装文件
这主要使用[Components]段实现,一个演示的代码如下: [setup] ;全局设置,本段必须 AppName=Test AppVerName=TEST DefaultDirName=" ...
- (转)Inno Setup入门(八)——有选择性的安装文件
本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250827 这主要使用[Components]段实现,一个演示的代 ...
- inno setup介绍及官方网站地址
使 用 笔 记 1.Inno Setup 是什么?Inno Setup 是一个免费的 Windows 安装程序制作软件.第一次发表是在 1997 年,Inno Setup 今天在功能设置和稳定性上的竞 ...
- Inno Setup脚本语法大全
Inno Setup脚本语法大全 ResourceShare Bruce 11个月前 (10-28) 6136浏览 0评论 Inno Setup 是什么?Inno Setup 是一个免费的 Win ...
- Inno Setup 使用笔记
使 用 笔 记https://blog.csdn.net/dongshibo12/article/details/79095971 1.Inno Setup 是什么?Inno Setup 是一个免费的 ...
- Inno Setup自定义卸载文件名称的脚本
Inno Setup 支持在同一个目录中安装多个应用程序,所以根据安装的先后次序自动将卸载程序文件命名为 unins000.exe,unins001.exe,unins002.exe 等等.这是 IN ...
- Inno Setup入门(一)——最简单的安装脚本
地址:http://379910987.blog.163.com/blog/static/3352379720110238252326/ 一个最简单的安装脚本: 1.最简单的安装文件脚本: [setu ...
- 本人亲测-inno setup打包EXE(较完整实例)
; Script generated by the Inno Setup Script Wizard.; SEE THE DOCUMENTATION FOR DETAILS ON CREATING I ...
随机推荐
- Material Design 组件之 CollapsingToolbarLayout
CollapsingToolbarLayout 主要用于实现一个可折叠的标题栏,一般作为 AppBarLayout 的子 View 来使用,下面总结一下 CollapsingToolbarLayout ...
- ssh-add和ssh-agent
注: 因为在ssh-agent异常关闭或者新开窗口是会导致ssh-add找不到私钥,导致添加的私钥无效,所以下面使用keychain管理 ssh-add 参数 -l 查看代理中的私钥 -L 查看代理中 ...
- 使用 Visual Studio 开发、测试和部署 Azure Functions(一)开发
1,什么是Azure functions Azure Functions 是 Microsoft Azure 提供的完全托管的 PaaS 服务,用于实现无服务器体系结构. Azure Function ...
- java Jsoup.clean 处理入参时,会将换行符解析成空字符串问题
Json 中clean方法有两个: 一:会格式化入参,将换行符替换成空格 clean(String bodyHtml, String baseUri, Whitelist whitelist) 二:n ...
- 从测试点点君跨入年薪30W的自动化逍遥君的人生感悟--测试君请进,绝对让你不虚此行!
一.前言:人生感悟 人生,就是一个苏醒的过程,生命就是一次历练,从鲜衣怒马,到银碗里盛雪,从青葱岁月到白发染鬓,人总是会在经历中成长,在成长中懂得,在懂得里看透,看透而不说透,从而一步一步的走向成熟, ...
- 使用Azure Rest API获得Access Token介绍
背景 本文主要介绍如何获取如何获取Azure Rest API的访问token,所采用的是v2.0版本的Microsoft标识平台,关于1.0和2.0的区别可以参考 https://docs.azur ...
- 路由与交换,cisco路由器配置,浮动静态路由
设置浮动静态路由的目的就是为了防止因为一条线路故障而引起网络故障.言外之意就是说浮动静态路由实际上是主干路由的备份.例如下图: 假如我们设路由器之间的串口(seria)为浮动静态路由(管理距离为100 ...
- Java第二天,类的概念,属性和方法的使用
上文中我们已近说到过了,Java是一种面向对象的编程语言,对象是用类来创建的,就比如世界上有无数个父亲,但是他们都有一个共同的属性--男人.也就是说某个父亲这个对象属于男人这个类.类是Java必不可少 ...
- "格式化的文本"组件:<span> —— 快应用原生组件
 `<template> <div class="container"> <text><span class="success ...
- 15-场景中用到的资源监视器(perfmon metrics collector)
JMeter 无法提取除 Tomcat 之外的其他服务器的指标,因此PerfMon Metrics Collector可用来获取性能数据. PerfMon Metrics Collector使用的是S ...