Delphi中擴充套件的字串操作函式

2021-08-26 01:48:30 字數 2945 閱讀 8792

procedure swapstr(var s1, s2: string); // 交換字串

function strright(str: string; len: integer): string; //返回字串右邊的字元 examples: strright('abcedfg',3);返回:'dfg'

function strleft(str: string; len: integer): string; //返回字串左邊的字元

function strnum(shortstr:string;longstring:string):integer; //返回某個字串中某個字串中出現的次數

function findstr(shortstr:string;longstring:string):integer; //返回某個字串中查詢某個字串的位置

function substr(psinput:string; beginplace,cutleng:integer):string;//返回從位置beginplace開始切取長度為catleng字串

function randomstr(alength : longint) : string; //隨機字串函式

function randomsys(num: integer): integer; //利用系統時間產生隨機數

function randomguid:string;//用guid得到乙個永遠不會重複的隨機序列

方法主體:

procedure swapstr(var s1, s2: string);

var tempstr: string;

begin

tempstr := s1;

s1 := s2;

s2 := tempstr;

end;

function strright(str: string; len: integer): string;

begin

if len >= length(str) then

result := str

else

result := copy(str, length(str) - len + 1, len);

end;

function strleft(str: string; len: integer): string;

begin

if len >= length(str) then

result := str

else

result := copy(str, 1, len);

end;

function strnum(shortstr:string;longstring:string):integer;

var i:integer;

begin

i:=0;

while pos(shortstr,longstring)>0 do

begin

i:=i+1;

longstring:=substr(longstring,(findstr(shortstr,longstring))+1,length(longstring)-findstr(shortstr,longstring))

end;

result:=i;

end;

function findstr(shortstr:string;longstring:string):integer;//在乙個字串中找某個字元的位置

var locality:integer;

begin

locality:=pos(shortstr,longstring);

if locality=0 then

result:=0

else

result:=locality;

end;

function substr(psinput:string; beginplace,cutleng:integer):string;

begin

result:=copy(psinput,beginplace,cutleng)

end;

function randomstr(alength : longint) : string;

var x : longint;

begin

if alength <= 0 then exit;

setlength(result, alength);

for x:=1 to alength do

result[x] := chr(random(26) + 65);

end;

function randomsys(num: integer): integer;

var t: _systemtime;

x: integer;

i: integer;

begin

result := 0;

if num = 0 then exit;;

getsystemtime(t);

x := trunc(t.wmilliseconds/10) * t.wsecond * 1231;

x := x + random(1);

if x<>0 then

x := -x;

x := random(x);

x := x mod num;

for i := 0 to x do

x := random(num);

result := x;

end;

function randomguid:string;

var id: tguid;

begin

if createguid(id) =0 then

begin

result := guidtostring(id);

end;

end;

Delphi 字串操作

常忘記,在此做筆記。這幾個函式都包含在strutils中,所以需要uses strutils 假設字串是 dstr delphi is the best 那麼 leftstr dstr,5 delph midstr dstr,6,7 i is th rightstr dstr,6 e best fu...

Delphi中的字串

delphi中的字串 一 各種字串 字串是object pascal所有資料型別中最有用的型別。許多函式以字串為傳遞引數。由於在delphi中字串的定義和使用有各種方式,包括pascal中典型的字串 string delphi支援的長字串 ansistring 類似於c語言的字元陣列 array o...

delphi字串操作集

delphi字串操作集 1.copy 字串的複製操作,有3個引數 copy 源資料,複製起始位置 含 複製長度 2.leftstr rightstr 所在單元 strutils 功能說明 返回字串左 右 邊指定個數的新字元 串 該函式有兩個引數。第乙個引數為完整的字串,第二個引數為指定個數。left...