TOP

Delphi中常用字符串处理函数(二)
2019-08-23 00:31:48 】 浏览:251
Tags:Delphi 常用 字符串 处理 函数

ource);
{删除source字符串} delete(s,site,StrLen); {插入target字符串到S中} insert(target,s,site); {返回新串} replacing:=s; end; /////////////////////// 另两个替换字符串中子串的函数 function repl_substr( sub1, sub2, s: string ): string; var i: integer; begin repeat i := pos( sub1, s ) ; if i > 0 then begin delete( s, i, Length(sub1)); insert( sub2, s, i ); end; until i < 1; Result := s; end; function ReplaceText(const S,ReplacePiece,ReplaceWith: String):String; Var Position: Integer; TempStr: String; begin Position := Pos(ReplacePiece,S); if Position > 0 then Begin TempStr := S; Delete(TempStr,1,Position-1+Length(ReplacePiece)); Result := Copy(S,1,Position-1)+ReplaceWith+ReplaceText(TempStr,ReplacePiece,ReplaceWith) End else Result := S; end; //////////////////////// 替换全部子字符串的函数 function ReplaceSub(str, sub1, sub2: String): String; var aPos: Integer; rslt: String; begin aPos := Pos(sub1, str); rslt := ''; while (aPos <> 0) do begin rslt := rslt + Copy(str, 1, aPos - 1) + sub2; Delete(str, 1, aPos + Length(sub1)); aPos := Pos(sub1, str); end; Result := rslt + str; end; ///////////////////////// 在字符串左右填充指定数量的指定字符 function UT_PadString(inString :string; maxLength :integer; padChar :char; left :boolean) :string; begin result := inString; while (Length(result) < maxLength) do if (left) then result := padChar + result else result := result + padChar; end; ///////////////////////////////////// 提取字符串中指定子字符串前的字符串 Function Before ( string ; Var S:string ) : string ; < /span> Var F : Word ; begin F := POS (Src,S) ; if F=0 then Before := S else Before := COPY(S,1,F-1) ; end ; ////////////////////////////////// 提取字符串中指定子字符串后的字符串 Function After ( string ; Var S:string ) : string ; < /span> Var F : Word ; begin F := POS (Src,S) ; if F=0 then After := '' else After := COPY(S,F+length(src),length(s)) ; end ; //////////////////////////////////// 判断字符串是否可以转换为整数 function IsIntStr(const S: string): boolean; begin Result:=StrToIntDef(S,0)=StrToIntDef(S,1); end; ////////////////////////////////////// 从字符串中删除指定字符串 procedure RemoveInvalid(what, where: string): string; var tstr: string; begin tstr:=where; while pos(what, tstr)>0 do tstr:=copy(tstr,1,pos(what,tstr)-1) + copy(tstr,pos(what,tstr)+length(tstr),length(tstr)); Result:=tstr; end; 用法: NewStr:=RemoveInvalid('<invalid>','This <invalid> is my string and I wan to remove the word <invalid>'); /////////////////////////////////////////// 根据某个字符分割字符串的函数 procedure SeparateTerms(s : string;Separator : char;Terms : TStringList); { This browses a string and divide it into terms whenever the given separator is found. The separators will be removed } var hs : string; p : integer; begin Terms.Clear; // First remove all remaining terms if Length(s)=0 then // Nothin' to separate Exit; p:=Pos(Separator,s); while P<>0 do begin hs:=Copy(s,1,p-1); // Copy term Terms.Add(hs); // Add to list Delete(s,1,p); // Remove term and separator p:=Pos(Separator,s); // Search next separator end; if Length(s)>0 then Terms.Add(s); // Add remaining term end; ========== = 用 法 ========== var Terms : TStringList; i : integer; const TestStr = '1st term;2

请关注公众号获取更多资料


Delphi中常用字符串处理函数(二) https://www.cppentry.com/bencandy.php?fid=86&id=229775

首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Delphi DBGrid类控件定位到某一行.. 下一篇delphi中WMI的使用(网卡是否接入..