程序如下:
type userarray=array of string;
function split(s:string;dot:char):userarray;
var
str:userarray;
i,j:integer;
begin
i:=1;
j:=0;
SetLength(str, 255);
while Pos(dot, s) > 0 do
begin
str[j]:=copy(s,i,pos(dot,s)-i);
i:=pos(dot,s)+1;
s[i-1] := chr(ord(dot)+1);
j:=j+1;
end;
str[j]:=copy(s,i,strlen(pchar(s))-i+1);
result:=str;
end;
这个函数的返回值如何定义,
a:userarray ;
a:=split(strpath1,'\');老是出错
type userarray=array of string;
function split(s:string;dot:char):userarray;
var
str:userarray;
i,j:integer;
begin
i:=1;
j:=0;
SetLength(str, 255);
while Pos(dot, s) > 0 do
begin
str[j]:=copy(s,i,pos(dot,s)-i);
i:=pos(dot,s)+1;
s[i-1] := chr(ord(dot)+1);
j:=j+1;
end;
str[j]:=copy(s,i,strlen(pchar(s))-i+1);
result:=str;
end;
这个函数的返回值如何定义,
a:userarray ;
a:=split(strpath1,'\');老是出错
str:userarray;它只是局部变量 函数执行完了 就没了 result:=str;
只是 简单的指向它可能错在这吧。
"result:=str;
只是 简单的指向它可能错在这吧。"
我觉得也可能是这个问题,数组付值付的是指针,你可以result.copy(str)试试。不过其实你返回自定义类型不好,还是返回VARIANT,方便引用
var
i,j:integer;
begin
i:=1;
j:=0;
SetLength(Result, 255);
while Pos(dot, s) > 0 do
begin
Result[j]:=copy(s,i,pos(dot,s)-i);
i:=pos(dot,s)+1;
s[i-1] := chr(ord(dot)+1);
j:=j+1;
end;
Result[j]:=copy(s,i,strlen(pchar(s))-i+1);
end;
function split(s:string;dot:char):userarray;
var
str:userarray;
i,j:integer;
begin
i:=1;
j:=0;
SetLength(str, 255);
while Pos(dot, s) > 0 do
begin
str[j]:=copy(s,i,pos(dot,s)-i);
i:=pos(dot,s)+1;
s[i-1] := chr(ord(dot)+1);
j:=j+1;
end;
str[j]:=copy(s,i,strlen(pchar(s))-i+1);
result:=str;
end;
function SHEventName(strPath1,strPath2:string;lParam:Integer):string;var a:userarray;
sEvent:String;begin
case lParam of
SHCNE_MKDIR: sEvent := '新建目录 目录名:'+strPath1;
a:=Split(strpath1,'\');
label1.caption:=a;
SHCNE_RENAMEFOLDER: sEvent := '重命名文件夹'+strPath1+'为'+strpath2;
else
sEvent:='未知操作'+IntToStr(lParam);
end; Result:=sEvent;
end;
是不是要在a之前指定a数组的大小
我晕.......TStringList 有这个功能。
sl: TStringList;
begin
sl := TStringList.Create;
sl.Delimiter := '@';
sl.DelimitedText := 'fj321 (321) @Delphi 语言基础/算法/系统设计@[email protected]';
//已经都放到sl里了,sl[0],sl[1]... 一个个取出来就是
sl.Free;
end;至于传数组,你可以用var把数组声明成变量参数,就是传址的形式,就可以了
procedure StrDivideToArr(AText: string; Delimiter: string; var arrResult: array of string);
var
k, j, lastpos: integer;
begin
k := low(arrResult);
lastpos := 1;
for j := 1 to length(AText) do
if AText[j] = Delimiter then
begin
arrResult[k] := Copy(AText, lastpos, j - lastpos);
lastpos := j + 1;
k := k + 1;
end;
arrResult[k] := Copy(AText, lastpos, length(AText) + 1 - lastpos);
end;