我正在慢慢地将现有代码转换为 Delphi 2010,并阅读了 Embarcaedro 网站上的几篇文章以及 Marco Cantú 白皮书。
还有一些我没有理解的东西,所以这里有两个函数来举例说明我的问题:
function RemoveSpace(InStr: string): string;
var
Ans : string;
I : Word;
L : Word;
TestChar: string[1];
begin
Ans := '';
L := Length(InStr);
if L > 0 then
begin
for I := 1 to L do
begin
TestChar := Copy(InStr, I, 1);
if TestChar <> ' ' then Ans := Ans + TestChar;
end;
end;
RemoveSpace := Ans;
end;
function ReplaceStr(const S, Srch, Replace: string): string;
var
I: Integer;
Source: string;
begin
Source := S;
Result := '';
repeat
I := Pos(Srch, Source);
if I > 0 then begin
Result := Result + Copy(Source, 1, I - 1) + Replace;
Source := Copy(Source, I + Length(Srch), MaxInt);
end
else Result := Result + Source;
until I <= 0;
end;
对于 RemoveSpace 函数,如果没有传递 unicode 字符(例如“aa bb”),则一切正常。现在,如果我传递文本“ab cd”,则该函数无法按预期工作(我得到 ab??cd 作为输出)。
如何解释字符串上可能的 unicode 字符?使用 Length(InStr) 和 Copy(InStr, I, 1) 显然是不正确的。
转换此代码以使其包含 unicode 字符的最佳方法是什么?
谢谢!