delphi中的字符串操作函数的使用(上)
2013-04-26 16:14
441 查看
1. 提取字符串中指定子字符串前的字符串
Function Before( Src:string ; S:string ): string ;
Var
F: Word ;
begin
F:= POS(Src,S) ;
if F=0 then
Before := S
else
Before := COPY(S,1,F-1) ;
end ;
eg: Before('123','helloworld_123') 返回结果:helloworld_
2. 提取字符串中指定子字符串后的字符串
function After(Src: string; S: string):string;
var
F: Word;
begin
F:= Pos(Src, S);
if F = 0 then
After:= ''
else
After:= Copy(S, F+Length(Src), Length(S));
end;
3. Delphi 替换函数
procedure Replace(var s:string;const SourceChar:pchar;const RChar:pchar);
//第一个参数是原串,第二个是模式串,第三个是替换串
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
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
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;
4. Delphi StringReplace() 替换字符串的用法
str:= '{"UserName":"helloworld","UserPass":"helloworld_123","UserEmail":"lovecode@163.com"}';
str:= StringReplace(str,'"','\"',[rfReplaceAll]);
StringReplace(源字符串,'被替换字符','替换后字符',[rfReplaceAll]);
5. 查找字符串中指定字符及字符串最后一次出现的位置
function RightPosEx(const Substr,S: string): Integer;
var
iPos: Integer;
TmpStr: string;
i,j,len: Integer;
PCharS,PCharSub: PChar;
begin
PCharS:=PChar(s); //将字符串转化为PChar格式
PCharSub:=PChar(Substr);
Result:=0;
len:=length(Substr);
for i:=0 to length(S)-1 do begin
for j:=0 to len-1 do begin
if PCharS[i+j]<>PCharSub[j] then break;
end;
if j=len then Result:=i+1;
end;
end;
调用方式:RightPosEx(‘\’,’123456\7\’);
Function Before( Src:string ; S:string ): string ;
Var
F: Word ;
begin
F:= POS(Src,S) ;
if F=0 then
Before := S
else
Before := COPY(S,1,F-1) ;
end ;
eg: Before('123','helloworld_123') 返回结果:helloworld_
2. 提取字符串中指定子字符串后的字符串
function After(Src: string; S: string):string;
var
F: Word;
begin
F:= Pos(Src, S);
if F = 0 then
After:= ''
else
After:= Copy(S, F+Length(Src), Length(S));
end;
3. Delphi 替换函数
procedure Replace(var s:string;const SourceChar:pchar;const RChar:pchar);
//第一个参数是原串,第二个是模式串,第三个是替换串
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
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
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;
4. Delphi StringReplace() 替换字符串的用法
str:= '{"UserName":"helloworld","UserPass":"helloworld_123","UserEmail":"lovecode@163.com"}';
str:= StringReplace(str,'"','\"',[rfReplaceAll]);
StringReplace(源字符串,'被替换字符','替换后字符',[rfReplaceAll]);
5. 查找字符串中指定字符及字符串最后一次出现的位置
function RightPosEx(const Substr,S: string): Integer;
var
iPos: Integer;
TmpStr: string;
i,j,len: Integer;
PCharS,PCharSub: PChar;
begin
PCharS:=PChar(s); //将字符串转化为PChar格式
PCharSub:=PChar(Substr);
Result:=0;
len:=length(Substr);
for i:=0 to length(S)-1 do begin
for j:=0 to len-1 do begin
if PCharS[i+j]<>PCharSub[j] then break;
end;
if j=len then Result:=i+1;
end;
end;
调用方式:RightPosEx(‘\’,’123456\7\’);
相关文章推荐
- Delphi字符串操作的常用函数四
- delphi字符串操作函数一览
- delphi字符串操作函数一览
- Delphi字符串操作的常用函数二
- DELPHI字符串、数组操作函数
- 编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数。
- 把参数字符串中的字符反向排列(不使用数组和库函数操作字符串的函数,不要局部数组来临时存储参数字符串)
- C语言 编写一个函数,将参数字符串中的字符反向排列。要求:不能使用C函数库中的字符串操作函数,
- Delphi学习之函数 ① 扩展的字符串操作函数
- DELPHI字符串、数组操作函数
- DELPHI字符串处理函数大全(收藏在此方便以后查找使用)
- Delphi字符串操作的常用函数三
- 使用安全的字符串操作函数 Strsafe.h
- 编写一个函数reverse_string(char * string)(递归实现) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中 的字符串操作函数。
- //4. 编写一个函数reverse_string(char * string)(递归实现) //实现:将参数字符串中的字符反向排列。 //要求:不能使用C函数库中的字符串操作函数。
- *Delphi字符串的基本操作与常用函数
- DELPHI 字符串、数组操作函数
- DELPHI 字符串、数组操作函数
- DELPHI字符串、数组操作函数
- Python学习13:字符串操作函数使用