找回密码
 立即注册
查看: 1555|回复: 0

Delphi中字符串形式为:/*/*/*/,如何才能取得中间的*哪?

[复制链接]

6802

主题

10

回帖

2万

积分

管理员

积分
21861
发表于 2019-5-20 21:45:19 | 显示全部楼层 |阅读模式
procedure TForm1.Button1Click(Sender: TObject);
var
   i : Integer;
   s : string;
begin
   with TStringList.Create do
   begin
       s := '/33/"321 424"/24234234/';
       Delimiter :='/';
       DelimitedText := Copy(s,2,Length(s)-2);
       for i:=0 to Count-1 do
       begin
           if Strings[i]<>'' then
           ShowMessage(Strings[i]);
       end;
   end;
end;

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

procedure TForm1.Button1Click(Sender: TObject);
var
   i : Integer;
   s : string;
begin
   with TStringList.Create do
   begin
       s := '/33/"321 424"/24234234/';
       Delimiter :='/';
       DelimitedText := Copy(s,2,Length(s)-2);
       for i:=0 to Count-1 do
       begin
           if Strings[i]<>'' then
           ShowMessage(Strings[i]);
       end;
       free;
   end;
end;

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

如果*中带有换行

那就不能用上面的方法,得遍历一遍
procedure TForm1.Button1Click(Sender: TObject);
var
   s,sTemp : string;
   i : Integer;
begin
   sTemp := '';
   s := '/33/321 424/2423'#10'4234/';
   for i:=2 to Length(s) do
   begin
       if s[i]='/' then
       begin
           ShowMessage(sTemp);
           sTemp :='';
       end
       else
           sTemp := sTemp+s[i];
   end;
end;

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

通常可以分步完成
设s0:='/abcd/efghij/klmop';

1.去掉s0的第1个'/':
s1:=copy(s0,2,length(s)-1);
则s1='abcd/efghij/klmop';

2.查出s0第2个'/'的位置, 也就是s1中第1个'/'的位置:
p:=pos('/',s1);
则 p=5;

3.求s1中/后面的子串:
 s2:=copy(s1,p,length(s1)-p);
则s2='efghij/klmop';

4.查出s0第3个'/'的位置, 也就是s2中'/'的位置:
p:=pos('/',s2);
则p=7;

5.求s3:=copy(s2,1,p-1);
则s3就是结果efghij了.

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

/**/
Function FillAStringList(Str : String; SL : TStrings):Boolean
Var
ret : Integer;
tmp_str : String;
Begin
ret := Pos('/',Str);
While ret > 0 do
Begin
   if ret = 1 then
     str := Copy(str,ret+1,MaxInt);
   else
     Begin
     tmp_str := Copy(str,1,ret-1);
     SL.add(tmp_str);      
     end
ret := Pos('/',Str);
end;
Result := True;
end;

//没测试

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

我写的函数,你试试好用不
//返回substr在sousestr中第index次出现的位置
function PosEx(subStr:string;SouseStr:string;Index:integer):integer;
var
i:integer;
iPos:integer;
AStr:string;
begin
AStr := SouseStr;
result := 0;
for i := 1 to Index do
begin
   iPos := Pos(subStr,AStr);
   if iPos = 0 then exit;
   if i = Index then
   begin
     result := length(SouseStr) - length(AStr) + iPos;
     exit;
   end;
   System.Delete(AStr,1,iPos + length(subStr) - 1);
end;
end;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表