Delphi - ExtractFileName messes up unicode
I am using ExtractFileName of a WideString that equals to 'STF:宋体'. The result is '??'.
It seems that ExtractFileName does not handle unicode well.
Is there an equivalent method for unicode?
Edit:
This is the line i use
NameStr := ExtractFileName(Name);
the types are:
NameStr:String;
Name:P开发者_StackOverflow社区WideChar
You can also use TntSysUtils.WideExtractFileName
from TNT.
You appear to be using an old version of Delphi without support for Unicode. The solution is to upgrade Delphi.
You can use a home-cooked version of ExtractFileName
, like this:
function ExtractFileNameW(FullPath:WideString):WideString;
var i,pos:Integer;
begin
// Find the last path separator
pos := -1;
for i:=Length(FullPath) downto 1 do
if (FullPath[i] = '/') or (FullPath[i] = '\') then
begin
pos := i;
Break;
end;
if pos = -1 then
Result := FullPath
else
begin
Result := '';
SetLength(Result, Length(FullPath) - pos);
System.Move(FullPath[pos+1], Result[1], (Length(FullPath) - pos) * SizeOf(WideChar));
end;
end;
... but if you really need to deal with Unicode characters you should upgrade to XE. It really makes a lot of difference.
Function ExtractFileNameW(const FN: widestring): widestring;
begin
Result := UTF8Decode(ExtractFileName(UTF8Encode(FN)));
end;
精彩评论