截取指定符号之间的字符串(随机读取)delphi实例代码

时间:2021-05-19

复制代码 代码如下:
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.

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章