建议楼主要学会看Delphi Help 帮助主题中输入:LastDelimiter VCL Reference LastDelimiter functionSee also Returns the byte index in S of the last character that matches any character in the Delimiters AnsiString.UnitSysUtilsCategorystring handling routinesfunction LastDelimiter(const Delimiters, S: string): Integer;DescriptionCall LastDelimiter to locate the last delimiter in S. For example, the lineMyIndex := LastDelimiter('\.:','c:\filename.ext');sets MyIndex to 12.When working with multi-byte character sets (MBCS), S may contain double byte characters, but the delimiters listed in the Delimeters parameter must all be single byte non-null characters.
//楼主的路径为什么要用“/” //难道是Kylix? const PathDelim = '/';function ExtractFileName(const FileName: string): string; var I: Integer; begin I := LastDelimiter(PathDelim + DriveDelim, FileName); Result := Copy(FileName, I + 1, MaxInt); end;function ChangeFileExt(const FileName, Extension: string): string; var I: Integer; begin I := LastDelimiter('.' + PathDelim + DriveDelim,Filename); if (I = 0) or (FileName[I] <> '.') then I := MaxInt; Result := Copy(FileName, 1, I - 1) + Extension; end;procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(ChangeFileExt(ExtractFileName('D:/文件/XX../图片-RGS.gif'), '')); end;
PathDelim = '/';function ExtractFileName(const FileName: string): string;
var
I: Integer;
begin
I := LastDelimiter(PathDelim + DriveDelim, FileName);
Result := Copy(FileName, I + 1, MaxInt);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(ExtractFileName('D:/文件/XX../图片-RGS.gif'));
end;
帮助主题中输入:LastDelimiter
VCL Reference
LastDelimiter functionSee also
Returns the byte index in S of the last character that matches any character in the Delimiters AnsiString.UnitSysUtilsCategorystring handling routinesfunction LastDelimiter(const Delimiters, S: string): Integer;DescriptionCall LastDelimiter to locate the last delimiter in S. For example, the lineMyIndex := LastDelimiter('\.:','c:\filename.ext');sets MyIndex to 12.When working with multi-byte character sets (MBCS), S may contain double byte characters, but the delimiters listed in the Delimeters parameter must all be single byte non-null characters.
ExtractFilePath
ExtractFileExt
这些函数都可以取出文件名中的相关部分,看看帮助里面怎么说的.
//难道是Kylix?
const
PathDelim = '/';function ExtractFileName(const FileName: string): string;
var
I: Integer;
begin
I := LastDelimiter(PathDelim + DriveDelim, FileName);
Result := Copy(FileName, I + 1, MaxInt);
end;function ChangeFileExt(const FileName, Extension: string): string;
var
I: Integer;
begin
I := LastDelimiter('.' + PathDelim + DriveDelim,Filename);
if (I = 0) or (FileName[I] <> '.') then I := MaxInt;
Result := Copy(FileName, 1, I - 1) + Extension;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(ChangeFileExt(ExtractFileName('D:/文件/XX../图片-RGS.gif'), ''));
end;
或者将其反转过来,从前面取,再搞回去,哈哈