Does anyone know how to trim a string/filename but keep the file extension?
For example:
I would like Picture1.jpg to become Pic.jpg .
I've been playing around with the StrUtils
unit without being able to work out a solution. LeftStr
was the first thing that came to mind.
Split the name into stem and extension. Shorten the stem. Then re-combine. Like this:
function ReduceFileName(const FileName: string; const MaxStemLength: Integer): string;
var
Ext: string;
StemLength: Integer;
begin
Assert(MaxStemLength > 0);
Ext := ExtractFileExt(FileName);
StemLength := Length(FileName) - Length(Ext);
if StemLength <= MaxStemLength then begin
Result := FileName;
exit;
end;
Result := Copy(FileName, 1, MaxStemLength) + Ext;
end;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.