{*****************************扩展的字符串操作函数*****************************} 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;