如何让函数返回一个数组?(30分)

  • 主题发起人 主题发起人 pub66cn
  • 开始时间 开始时间
P

pub66cn

Unregistered / Unconfirmed
GUEST, unregistred user!
我想取用一个函数处理字符串如:
1111>>>2222>>>3333>>
把上面>>>分隔的字符提出来后保存在一个数组中,然后返回,
delphi怎么实现呀?
 
用TStrList类作为函数返回类型就可以了
 
type
mya = array of string;

implementation
function f():mya;
begin
Setlength(Result, 100);
end;


function f2(var a : mya):boolean
 
还是不很明,请各位多码两个字
 
type
TmyArray = array of string;

implementation
procedure aaa(var aArray : TMyArray);
begin
Setlength(aArray, 100);
.......
end;

procedure bbb;
var
i
integer;
theArray : TmyArray;
begin
aaa(theArray);
for i := Low(theArray) to High(theArray) do
begin
.....
end;
end;
 
窗体资源代码如下:
object Form1: TForm1
Left = 192
Top = 112
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object lbl1: TLabel
Left = 104
Top = 24
Width = 16
Height = 13
Caption = 'lbl1'
end
object btn1: TButton
Left = 104
Top = 136
Width = 75
Height = 25
Caption = 'btn1'
TabOrder = 0
OnClick = btn1Click
end
object edt1: TEdit
Left = 280
Top = 80
Width = 121
Height = 21
TabOrder = 1
Text = '111>>>222>>>333>>>'
end
end
程序单元代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
btn1: TButton;
lbl1: TLabel;
edt1: TEdit;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
function GetList(const aStr: String
const aSep: String): TStringList;
var
strTmp: String;
intPos: Integer;
begin
Result := TStringList.Create;
strTmp := aStr;
with Result do
begin
while Length(strTmp) > 1 do
begin
intPos := Pos(aSep,strTmp);
Add(Copy(strTmp,1,intPos - 1));
strTmp := Copy(strTmp,intPos + Length(aSep),Length(strTmp));
end;
end;
end;
var
I: Integer;
astrlistTmp: TStringList;
begin
//astrlistTmp := TStringList.Create;
astrlistTmp := GetList(edt1.Text,'>>>');
for I := 0 to astrlistTmp.Count - 1 do
ShowMessage(astrlistTmp.Strings);
end;

end.
 
1.var a1:array of string;
2.setlength(a1,3)(3,是通过分隔符>>>来获取的)
3:a1[0]:=s;
var array of string;
s1:string;
len:integer;
s_temp:string;
begin
s1:='a>>>b>>>c';
s_temp:='';
len:=0;
while s1<>'' do
begin
inc(len);
setlength(a1,len);
//处理取直存放到s_temp;
a1[len-1]:=s_temp;
//对s1处理
end;
end;
大概是这样吧,其他的自己写吧
 
感谢大家帮忙,问题解决,散分。
 
后退
顶部