function LMDReplaceChar(const aValue:String; toReplace, Replace:Char):String;
var
i:Integer;
begin
result:=aValue;
for i:=1 to Length(result) do
if result[i]=toReplace then result[i]:=Replace;
end;
too easy!
function trimchar(Str,char:string):String;
begin
while Pos(char,Str)>0 do
Str:=copy(Str,1,pos(char,Str)-1)+copy(Str,pos(char,Str)+1,Length(Str));
Result:=Str;
end;
来自Rxlib的strutils:
function DelSpace(const S: string): string;
{ DelSpace return a string with all white spaces removed. }
function DelChars(const S: string; Chr: Char): string;
{ DelChars return a string with all Chr characters removed. }
function DelChars(const S: string; Chr: Char): string;
var
I: Integer;
begin
Result := S;
for I := Length(Result) downto 1 do begin
if Result[I] = Chr then Delete(Result, I, 1);
end;
end;
function DelSpace(const S: String): string;
begin
Result := DelChars(S, ' ');
end;
function LeftStr(OrgStr: string; CharCount: smallint): string;
begin
try
result:=Copy(OrgStr,0,CharCount);
except
result:='';
end;
end;
function RightStr(OrgStr: string; CharCount: smallint): string;
begin
result:=Copy(OrgStr,(Length(OrgStr)-CharCount)+1,Length(OrgStr));
end;
function DeleteChars(OrgStr: string; CharPos, CharCount: smallint): string;
begin
result:=LeftStr(OrgStr,CharPos)+RightStr(OrgStr,Length(OrgStr)-CharPos-CharCount);
end;
function InsertChars(OrgStr, InsChars: string; CharPos : smallint): string;
begin
result:=LeftStr(OrgStr,CharPos)+InsChars+RightStr(OrgStr,Length(OrgStr)-CharPos);
end;
function ReplaceChars(OrgStr, ReplChars: string; CharPos : smallint): string;
begin
result:=LeftStr(OrgStr,CharPos)+ReplChars+RightStr(OrgStr,Length(OrgStr)-CharPos-Length(ReplChars));;
end;
function ReplaceChars2(OrgStr: string; ReplChar: Char; CharPos, CharCount : smallint): string;
var x: smallint;
begin
result:=OrgStr;
for x:=0 to CharCount-1 do
begin
result:=ReplaceChars(result,ReplChar,CharPos+x);
end;
end;
好象没有函数可以这么做,但是我可以有一个算法给你,除空格,或者指定的字符也行:
function trimchar(S,ch:string);
i:=pos(ch,s);
While i<>0 do
begin
delete(S,I,1);
i:=pos(ch,s);
end
其中S为你处理的字符串,ch为需要去除的字符。