sma*_*ins 1 delphi unicode delphi-2009 delphi-2010
我正在慢慢将现有代码转换为Delphi 2010,并阅读Embarcaedro网站上的几篇文章以及MarcoCantú白皮书.
还有一些我还没有理解的东西,所以这里有两个函数来举例说明我的问题:
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;
Run Code Online (Sandbox Code Playgroud)
对于RemoveSpace函数,如果没有传递unicode字符(例如'aa bb'),一切都很好.现在,如果我传递文本'ab cd',那么函数不能按预期工作(我得到ab ?? cd作为输出).
如何计算字符串上可能的unicode字符?使用长度(InStr)显然不正确以及复制(InStr,I,1).
转换此代码以使其占用unicode字符的最佳方法是什么?
谢谢!
zz1*_*433 14
如果那些是你的真实功能而你只是想让它们工作,那么:
function RemoveSpace(const InStr: string): string;
begin
Result := StringReplace(InStr, ' ', '', [rfReplaceAll]);
end;
function ReplaceStr(const S, Srch, Replace: string): string;
begin
Result := StringReplace(S, Srch, Replace, [rfReplaceAll, rfIgnoreCase]);
end;
Run Code Online (Sandbox Code Playgroud)