问题描述
我正在慢慢地将现有的代码转换成Delphi 2010,并阅读了Embarcaedro网站上的几篇文章以及MarcoCantú白皮书。还有一些我没有理解的东西,所以这里有两个例子来说明我的问题:
function RemoveSpace(InStr:string):string ;
var
Ans:string;
I:Word;
L:Word;
TestChar:string [1];
begin
Ans:='';
L:=长度(InStr);
如果L> 0 then
begin
for I:= 1 to L do
begin
TestChar:= Copy(InStr,I,1);
如果TestChar<> ''然后Ans:= Ans + TestChar;
结束
结束
RemoveSpace:= Ans;
结束
函数ReplaceStr(const S,Srch,Replace:string):string;
var
I:整数;
资料来源:string;
begin
资料来源:= S;
结果:='';
重复
I:= Pos(Srch,Source);
如果我> 0然后开始
结果:=结果+复制(源,1,I - 1)+替换;
资料来源:= Copy(Source,I + Length(Srch),MaxInt);
end
else Result:= Result + Source;
,直到I 结束
对于RemoveSpace函数,如果没有unicode字符被传递(例如'aa bb'),则很好。现在如果我通过文本'ab cd',那么该函数不能按预期的方式工作(我得到ab ?? cd作为输出)。
对于字符串可能的unicode字符?使用长度(InStr)显然不正确以及复制(InStr,I,1)。
转换此代码的最佳方法是使其占用unicode字符
谢谢!
如果这些是您的REAL功能,你只是试图让em工作,然后:
function RemoveSpace(const InStr:string):string;
begin
结果:= StringReplace(InStr,'','',[rfReplaceAll]);
结束
函数ReplaceStr(const S,Srch,Replace:string):string;
begin
结果:= StringReplace(S,Srch,Replace,[rfReplaceAll,rfIgnoreCase]);
结束
I'm slowly converting my existing code into Delphi 2010 and read several of the articles on Embarcaedro web site as well as Marco Cantú whitepaper.
There are still some things I haven't understood, so here are two functions to exemplify my question:
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;
For the RemoveSpace function, if no unicode character is passed ('aa bb' for example), all is well. Now if I pass the text 'ab cd' then the function doesn't work as expected (I get ab??cd as the output).
How can I account for possible unicode characters on a string? using Length(InStr) is obviously incorrect as well as Copy(InStr, I, 1).
What's the best way of converting this code so that it accounts for unicode characters?
Thanks!
If those were your REAL functions and you're just trying to get em working then :
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;
这篇关于转换功能到delphi 2009/2010(unicode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!