例如:
var a:pchar;
i:integer;
begin
a:='abcdef';
for i:= 1 to 6 do
begin
分别读出a,b,c,d,e,f
end;
end;
请不吝赐教,小弟感激不敬!
...全文
18810打赏收藏
请教关于pchar的小问题
请问各位大虾,怎样循环读出pchar串中的每个字符? 也就是怎样像string[index]那样读? 例如: var a:pchar; i:integer; begin a:='abcdef'; for i:= 1 to 6 do begin 分别读出a,b,c,d,e,f end; end; 请不吝赐教,小弟感激不敬!
function NthChar(s: PChar; index: cardinal): Char;
begin
if index > StrLen(s) then
raise ERangeError.Create('Index is out of range.');
Result:=(s+index)^; // index is 0 based
end;
测试代码:
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
try
for i :=0 to 10 do // will run out of range!
begin
ShowMessage(NthChar(PChar(Caption),0));
end;
except
On E: Exception do // but here we can intercept it
ShowMessage(E.Message);
end;
end;