求教关于字符串替换
----------------------
如何将一字符串中指定字符串替换为另外一指定字符串?原字符串很长,有几千个字符.
------------------
参考下边函数:
function Replace(var s:string;const SourceChar:pchar;const RChar:pchar):string;
//第一个参数是原串,第二个是模式串,第三个是替换串
var
ta,i,j:integer;
m,n,pn,sn:integer;
SLen,SCLen,RCLen:integer;//SLen表示原串的长度,SCLen表示模式传的长度,RCLen表示替换串的长度
IsSame:integer;
newp:array of char;//用来保存替换后的字符数组
begin
SLen:=strlen(pchar(s));SCLen:=strlen(SourceChar);RCLen:=strlen(RChar);
j:=pos(string(SourceChar),s);
s:=s+chr(0);ta:=0;i:=j;
while s[i]<>chr(0) do //这个循环用ta统计模式串在原串中出现的次数
begin
n:=0;IsSame:=1;
for m:=i to i+SCLen-1 do
begin
if m>SLen then begin IsSame:=0;break; end;
if s[m]<>sourceChar[n] then begin IsSame:=0;break; end;
n:=n+1;
end;
if IsSame=1 then begin ta:=ta+1;i:=m; end else i:=i+1;
end;
if j>0 then
begin
pn:=0;sn:=1;
setlength(newp,SLen-ta*SCLen+ta*RCLen+1);//分配newp的长度,+1表示后面还有一个#0结束符
while s[sn]<>chr(0) do //主要循环,开始替换
begin
n:=0;IsSame:=1;
for m:=sn to sn+SCLen-1 do //比较子串是否和模式串相同
begin
if m>SLen then begin IsSame:=0;break; end;
if s[m]<>sourceChar[n] then begin IsSame:=0;break; end;
n:=n+1;
end;
if IsSame=1 then//相同
begin
for m:=0 to RCLen-1 do
begin
newp[pn]:=RChar[m];pn:=pn+1;
end;
sn:=sn+SCLen;
end
else
begin //不同
newp[pn]:=s[sn];
pn:=pn+1;sn:=sn+1;
end;
end;
newp[pn]:=#0;
s:=string(newp); //重置s,替换完成! end;
end;
----------------------
如何将一字符串中指定字符串替换为另外一指定字符串?原字符串很长,有几千个字符.
------------------
参考下边函数:
function Replace(var s:string;const SourceChar:pchar;const RChar:pchar):string;
//第一个参数是原串,第二个是模式串,第三个是替换串
var
ta,i,j:integer;
m,n,pn,sn:integer;
SLen,SCLen,RCLen:integer;//SLen表示原串的长度,SCLen表示模式传的长度,RCLen表示替换串的长度
IsSame:integer;
newp:array of char;//用来保存替换后的字符数组
begin
SLen:=strlen(pchar(s));SCLen:=strlen(SourceChar);RCLen:=strlen(RChar);
j:=pos(string(SourceChar),s);
s:=s+chr(0);ta:=0;i:=j;
while s[i]<>chr(0) do //这个循环用ta统计模式串在原串中出现的次数
begin
n:=0;IsSame:=1;
for m:=i to i+SCLen-1 do
begin
if m>SLen then begin IsSame:=0;break; end;
if s[m]<>sourceChar[n] then begin IsSame:=0;break; end;
n:=n+1;
end;
if IsSame=1 then begin ta:=ta+1;i:=m; end else i:=i+1;
end;
if j>0 then
begin
pn:=0;sn:=1;
setlength(newp,SLen-ta*SCLen+ta*RCLen+1);//分配newp的长度,+1表示后面还有一个#0结束符
while s[sn]<>chr(0) do //主要循环,开始替换
begin
n:=0;IsSame:=1;
for m:=sn to sn+SCLen-1 do //比较子串是否和模式串相同
begin
if m>SLen then begin IsSame:=0;break; end;
if s[m]<>sourceChar[n] then begin IsSame:=0;break; end;
n:=n+1;
end;
if IsSame=1 then//相同
begin
for m:=0 to RCLen-1 do
begin
newp[pn]:=RChar[m];pn:=pn+1;
end;
sn:=sn+SCLen;
end
else
begin //不同
newp[pn]:=s[sn];
pn:=pn+1;sn:=sn+1;
end;
end;
newp[pn]:=#0;
s:=string(newp); //重置s,替换完成! end;
end;
解决方案 »
- 获得系统字体问题。
- delphi自定义的过程,函数作不作为窗体函数有什么区别?
- 菜鳥關於ReadInteger method (TRegIniFile)
- 怎样让程序重起
- 关于在自已的程序中实现对控制台的完整封装
- 用友政务Delphi软件工程师(人数:5)[email protected] 010-82613388-5002 别删好不好
- 散分
- 大家帮帮忙,有关Dll的问题。
- 在Form1中引用tremotedatamodule对象(向里面加了adoconnection对象,connectionstring属性也配置好了)但读不出来,是什么原因。
- 为什么我用ADOQUERY不能实现批刷新,只能一条一条POST,各位大侠帮帮忙?
- 要将本地许多图片同时上传到服务器,且不上传已经有的同名图片,怎么实现?
- 有关默认打印机的问题,请高手指点。
调试发现,这一步newp的值没有赋给s,s为空值.
StringReplace(sString, 'ff', 'aa' ,[rfReplaceAll, rfIgnoreCase])
把 ff替换成 aa
StringReplace(ss, 'a', 'b' ,[rfReplaceAll, rfIgnoreCase]);
执行了.ss没变
ss := StringReplace(ss, 'a', 'b' ,[rfReplaceAll, rfIgnoreCase]);
ShowMessage(ss);