gpt4 book ai didi

delphi - 修剪文件名但保留文件扩展名

转载 作者:行者123 更新时间:2023-12-02 04:02:31 25 4
gpt4 key购买 nike

有人知道如何修剪字符串/文件名但保留文件扩展名吗?

例如:

我希望将 Picture1.jpg 变为 Pic.jpg

我一直在使用 StrUtils 单元,但无法找到解决方案。 LeftStr 是我首先想到的。

最佳答案

将名称拆分为词干和扩展名。缩短茎。然后重新组合。像这样:

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;

关于delphi - 修剪文件名但保留文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35943930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com