//xxxx//yyyy//zzzz,这样的字符串如何取出xxxx,yyyy和zzzz(0分)

  • 主题发起人 forever_chen
  • 开始时间
F

forever_chen

Unregistered / Unconfirmed
GUEST, unregistred user!
//xxxx//yyyy//zzzz,这样的字符串如何取出xxxx,yyyy和zzzz
 
用Pos函数和Copy函数就可以取出了
不过外加一个循环
 
能不能帮我写一下
 
一个通用函数
function Compose(str: string;
fgf: string;
Num: Integer): string;
var
TempI: Integer;
ResultNum: Integer;
begin
ResultNum := 0;
if Num = 0 then
begin
while Truedo
begin
TempI := Pos(fgf, Str);
if TempI <> 0 then
begin
Inc(ResultNum);
Str := Copy(Str, TempI + Length(fgf), Length(Str));
end
else
Break;
end;
Result := IntToStr(ResultNum + 1);
end
else
begin
for TempI := 2 to Numdo
begin
ResultNum := Pos(fgf, str {Result});
if ResultNum = 0 then
str := ''
else
str := Copy(str, ResultNum + Length(fgf), Length(str));
end;
Result := Str;
ResultNum := Pos(fgf, str);
if ResultNum <> 0 then
Result := Copy(str, 1, ResultNum - 1);
end;
end;

对于你给出的
const testStr='//xxxx//yyyy//zzzz';
result0 := Compose(testStr,'//',0);{4};{返回用分隔符分成的断数}
result1 := Compose(testStr,'//',2);{xxxx};
result2 := Compose(testStr,'//',3);{yyyy};
result3 := Compose(testStr,'//',4);{zzzz};
 
//xxxx//yyyy//zzzz,这样的字符串如何取出xxxx,yyyy和zzzz
觉得像在C(++)语言下的字串,你的//第一个/是不是转义字符?如果是就好办了
在Delphi, CBuilder中你可以使用一个TStringList对象 stTest;
设置
stTest.Delimiter := '/';
stTest.DelimitedTest := '//xxxx//yyyy//zzzz';
for i := 0 to stTest.Count
begin
stTest...;
end;

如果在VC++下用CStringT
The following example demonstrates the use of CStringT::Tokenize.
//typedef CStringT< TCHAR, StrTraitATL< TCHAR > > CAtlString;
CAtlString str( "%First Second#Third" );
CAtlString resToken;
int curPos= 0;
resToken= str.Tokenize("% #",curPos);
while (resToken != "")
{
printf("Resulting token: %s/n", resToken);
resToken= str.Tokenize("% #",curPos);
};
Output
Resulting Token: First
Resulting Token: Second
Resulting Token: Third
 
简单点的程序
procedure TForm1.Button1Click(Sender: TObject);
var
tmpstr:string;
begin
tmpstr:='//xxxx//yyyy//zzzz';
Memo1.Lines.Clear;
while Pos('//',tmpstr)>0do
begin
tmpstr:=Copy(tmpStr,Pos('//',TmpStr)+2,Length(TmpStr));
if Pos('//',TmpStr)>0 then
Memo1.Lines.Add(Copy(TmpStr,1,Pos('//',TmpStr)-1))
else
Memo1.Lines.Add(TmpStr);
end;
end;

 

Similar threads

回复
0
查看
697
不得闲
回复
0
查看
882
不得闲
回复
0
查看
827
不得闲
S
回复
0
查看
977
swish
S
顶部