delphi 怎样获取文件名不带后缀

2025-05-07 15:35:55
推荐回答(4个)
回答1:

小写了一个函数,取文件名的。

function GetFileName(str:string):string;
var
i:Integer;
x,y:string;
begin
x:=ExtractFileName(str); //取文件名+扩展名,不包含文件路径
y:=ExtractFileExt(str); //取文件的扩展名
i:=Pos(y,x); //确定扩展名所在位置
if i<>0 then
begin
Result:=Copy(x,0,i-1); //复制文件名,忽略掉后面的扩展名
end
else begin
Result:='';
end;
end;

使用的时候直接这样就行
GetFileName(TIdAttachment(Msg.MessageParts.Items[intIndex]).Filename)
输出:科比

回答2:

写一个for语句,从右往左,取出第一个“.”的位置<比如str="科比.jpg",取出“.”的位置x>,然后输出字符串味:str的第一个到第x个字符,即str[0]到str[x-1]。

这是我大致的思路。具体代码楼主应该可以写的出来。

回答3:

取文件名ExtractFileName(FileName);
取文件扩展名:ExtractFileExt(filename);
取文件名,不带扩展名Function ExtractFileNameNoExt(FileString: String): String;
Var
FileWithExtString: String;
FileExtString: String;
LenExt: Integer;
LenNameWithExt: Integer;
Begin
FileWithExtString := ExtractFileName(FileString);
LenNameWithExt := Length(FileWithExtString); FileExtString := ExtractFileExt(FileString); LenExt := Length(FileExtString);
If LenExt = 0 Then
Begin
Result := FileWithExtString;
End
Else
Begin
Result := Copy(FileWithExtString,1,(LenNameWithExt-LenExt));
End;
End;

回答4:

ChangeFileExt(TIdAttachment(Msg.MessageParts.Items[intIndex]).Filename,'');