急死我了!字符串问题,请各位帮忙!不胜感激!只有5分了,不好意思! (5分)

A

abcxyz

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟初学,代码很乱,不好意思。以下是我的代码,
目的是找出str中所有扩展名为.bat的文件名,如本例中应是abcde.bat和poiut.bat。
但我写得这段代码只能找出第一个.bat的文件名abcde.bat,后面一个就不行了,
怎样才能把它们都找出来并且都保存下来呢?注意事先不知道有多少个.bat,
本例有2个只是举的例子。
急啊!求各位快帮忙吧!
非常感谢!
var
Form1: TForm1;
str: string;
MyChar: string;
p, pTmp: Integer;
L: Integer;
pLen: Integer;
MyStrList: TStringList;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
MyStrList:=TStringList.Create;
str:='abcdefghi=abcde.bat qwertyioj/poiut.bat';
L:=Length(str);
p:=pos('.bat',str);
pTmp:=p;
MyChar:=copy(str,p+4,1);
if (MyChar='"') or (MyChar=' ') or (MyChar='>') then
begin
while (pTmp>0) and (str[pTmp]<>'/') and (str[pTmp]<>'/') and
(str[pTmp]<>'=') and (str[pTmp]<>'"')do
begin
Dec(pTmp);
end;
if (str[pTmp]='/') or (str[pTmp]='/') or
(str[pTmp]='=') or (str[pTmp]='"') then
begin
pLen:=(L-pTmp)-(L-(p+3));
MyStrList.Add(copy(str,pTmp+1,pLen));
delete(str,1,p+3);
end;
end;
end;
 
只有5分,懒得翻程序了。
 
以后再给你们加分,先帮帮我啊!
 
怎么没人告诉我?
 
是你的 if 语句有问题
 
问题已解决,虽只有5分,但我不想给白河愁加,谁要,留个言。
 
你的 if 语句造成第2次循环无法进入
好好看看吧
 
procedure TForm1.Button1Click(Sender: TObject);
begin
MyStrList:=TStringList.Create;
str:='abcdefghi=abcde.bat qwertyioj/poiut.bat';
while Pos('.bat',Str)>0do
begin
p:=Pos('.bat',Str)-1;
s:='.bat';
while (p>0) and not (Str[p] in ['/','/','=','"'])do
{可自行调整非文件名字符集}
begin
s:=Str[p]+s;
Dec(p);
end;
MyStrList.Add(S);
Delete(Str,1,p+5)
end;
end;
 
接受答案了.
 
这是不是你要的!5分是少了一点,不过没关系,快给分吧~~~~:)
procedure TForm1.Button1Click(Sender: TObject);
var
str: string;
MyStrList: TStringList;
p,pTmp: Integer;
MyChar: Char;
begin
MyStrList:=TStringList.Create;
try
str:='abcdefghi=abcde.bat qwertyioj/poiut.bat abcdefj"aaa.bat';
p:=pos('.bat',str);
while p > 0do
begin
pTmp := p;
while (pTmp>0) and (str[pTmp]<>'/') and (str[pTmp]<>'/') and
(str[pTmp]<>'=') and (str[pTmp]<>'"')do
begin
Dec(pTmp);
end;
MyStrList.Add(copy(str,pTmp + 1, p -pTmp + 3));
MyChar:=str[p+4];
if (MyChar='"') or (MyChar=' ') or (MyChar='>') then
Delete(str,1,p+4) else
Delete(str,1, p+3);
p:=pos('.bat',str);
end;
ListBox1.Items := MyStrList;
finally
MyStrList.Free;
end;
end;
 
这样的程序效率也太低了。
 
上面老兄胡闹 ,人家让你该错
问题在 你的 '''' 写成' " '了
 
to 白河愁:
呵呵,效率是低了一点,我只是在他原有的基础上改的,没有优化,
你好像是那种深藏不露的人啊!不如也露一手吧!:)
 
顶部