Delphi中For In 語法應用例項
阿新 • • 發佈:2019-02-08
一、遍歷 TStrings
var
List: TStrings;
s: string;
begin
List := TStringList.Create;
List.CommaText := 'aaa,bbb,ccc';
for s in List do
ShowMessage(s);
List.Free;
end;
二、遍歷陣列
var
Arr: array[0..2] of Byte;
i: Integer;
b: Byte;
begin
for i := Low(Arr) to High(Arr) do
Arr[i] := Random(MAXBYTE);
for b in Arr do
ShowMessage(IntToStr(b));
end;
三、遍歷子界
{例1}
var
sub: 0..9;
str: string;
begin
str := '';
for sub in [Low(sub)..High(sub)] do
str := str + IntToStr(sub);
ShowMessage(str); {0123456789}
end;
{例2}
type
TSub = 'A'..'G';
var
sub: TSub;
str: string;
begin
str := '';
for sub in [Low(sub)..High(sub)] do
str := str + sub;
ShowMessage(str); {ABCDEFG}
end;
{例3}
var
sub: Byte; {Byte 應該算是個 0..255 的子界}
num: Cardinal;
begin
num := 0;
for sub in [Low(sub)..High(sub)] do
Inc(num, sub);
ShowMessage(IntToStr(num)); {32640}
end;
四、遍歷列舉
type
TEnum = (Red,Green,Blue);
var
enum: TEnum;
count: Integer;
begin
count := 0;
for enum in [Low(enum)..High(enum)] do
Inc(count);
ShowMessage(IntToStr(count)); {3}
end;
五、遍歷集合
type
TEnum = (Red,Green,Blue,Yellow);
TSet = set of TEnum;
var
set1: set of TEnum;
set2: TSet;
elem: Tenum;
count: Integer;
begin
set1 := [Red, Yellow];
count := 0;
for elem in set1 do Inc(count);
ShowMessage(IntToStr(count)); {2}
set2 := [Red..Yellow];
count := 0;
for elem in set2 do Inc(count);
ShowMessage(IntToStr(count)); {4}
end;
六、遍歷字串
var
str: string;
c: Char;
begin
str := 'ABCD';
for c in str do
ShowMessage(c);
end;
轉自:http://hi.baidu.com/xuanyan/blog/item/ab0881cb74ae2bdd53664f0c.html
var
List: TStrings;
s: string;
begin
List := TStringList.Create;
List.CommaText := 'aaa,bbb,ccc';
for s in List do
ShowMessage(s);
List.Free;
end;
二、遍歷陣列
var
Arr: array[0..2] of Byte;
i: Integer;
b: Byte;
begin
for i := Low(Arr) to High(Arr) do
Arr[i] := Random(MAXBYTE);
for b in Arr do
ShowMessage(IntToStr(b));
end;
三、遍歷子界
{例1}
var
sub: 0..9;
str: string;
begin
str := '';
for sub in [Low(sub)..High(sub)] do
str := str + IntToStr(sub);
ShowMessage(str); {0123456789}
end;
{例2}
type
TSub = 'A'..'G';
var
sub: TSub;
str: string;
begin
str := '';
for sub in [Low(sub)..High(sub)] do
str := str + sub;
ShowMessage(str); {ABCDEFG}
end;
{例3}
var
sub: Byte; {Byte 應該算是個 0..255 的子界}
num: Cardinal;
begin
num := 0;
for sub in [Low(sub)..High(sub)] do
Inc(num, sub);
ShowMessage(IntToStr(num)); {32640}
end;
四、遍歷列舉
type
TEnum = (Red,Green,Blue);
var
enum: TEnum;
count: Integer;
begin
count := 0;
for enum in [Low(enum)..High(enum)] do
Inc(count);
ShowMessage(IntToStr(count)); {3}
end;
五、遍歷集合
type
TEnum = (Red,Green,Blue,Yellow);
TSet = set of TEnum;
var
set1: set of TEnum;
set2: TSet;
elem: Tenum;
count: Integer;
begin
set1 := [Red, Yellow];
count := 0;
for elem in set1 do Inc(count);
ShowMessage(IntToStr(count)); {2}
set2 := [Red..Yellow];
count := 0;
for elem in set2 do Inc(count);
ShowMessage(IntToStr(count)); {4}
end;
六、遍歷字串
var
str: string;
c: Char;
begin
str := 'ABCD';
for c in str do
ShowMessage(c);
end;
轉自:http://hi.baidu.com/xuanyan/blog/item/ab0881cb74ae2bdd53664f0c.html