平时做技术实践时,很多问题不是概念不会,而是细节没串起来。拿“截取指定符号之间的字符串(随机读取)delphi实例代码”来说,它看着像小点,放到项目里常会牵出环境、配置、兼容性和维护成本。下面按实际采用顺序,把思路、关键写法和容易踩坑的地方讲清楚,便于大家直接对照操作。
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
test: TMemo;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function PosEx(const Source, Sub: string; Index: integer): integer;
var
Buf : string;
i, Len, C : integer;
begin
C := 0;
Result := 0;
Buf := Source;
i := Pos(Sub, Source);
Len := Length(Sub);
while i <> 0 do
begin
inc(C);
Inc(Result, i);
Delete(Buf, 1, i + Len - 1);
i := Pos(Sub, Buf);
if C >= Index then Break;
if i > 0 then Inc(Result, Len - 1);
end;
if C < Index then Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i,y:integer;
x,c:string;
g,g1:integer;
begin
randomize; //生成随机数种子
i:=random(30);
y:=i+1;
c:='topfox000|topfox001|topfox002|topfox003|topfox004|topfox005|topfox006|topfox0007|topfox008|topfox009|tellyoumysecret000|tellyoumysecret002|tellyoumysecret003|tellyoumysecret004|tellyoumysecret005|tellyoumysecret006|'+
'onhacker046|onionhacker047|onionhacker048|onionhacker049|onionhacker140|onionhacker141|onionhacker142|onionhacker143|onionhacker144';
test.text:=test.text+inttostr(PosEx( c, '|',i));//得到5
g:= PosEx( c, '|',i)+1;//得到5
test.text:=test.text+inttostr(PosEx( c, '|',y));//得到5
g1:=PosEx( c, '|',y);//得到5
x:=Copy(c,g,g1-g);
memo1.text:=x;
end;
end.
- Delphi7中群发Email邮件的方法
- delphi实现保存和读取图片的方法
- Delphi远程连接Mysql的实现方法
- Delphi新建开机启动项的方法示例
- Delphi编程常用快捷键大全
- Delphi实现拿到句柄并发送消息的方法
- Delphi实现木马文件传输代码实例
- Delphi实现限定软件采用时间的方法
- delphi字符串分隔函数用法实例