Excelのcountif関数のような事は出来ますでしょうか?
例えば値が100以下のセル数を取得するといった感じです。
TStringGridには、その関数のようなメソッドは、ありませんので、
該当する列のセルが、条件に合うかどうかを、ループで1つ1つ確認するのがよいかと思います。
相当久しぶりに書き込みます。
あると便利そうなので、それらしいのを作ってみました。
※何分まだ未熟者ですので、この程度の簡易なものが限界ですが・・・。
count_if_int(
StringList;
'='or'>'or'<'or'>='or'<='or'<>'の何れかを指定;
比較対象の数字
):返値(見つかった個数)
使用例:showmessage( count_if_int( st,'<',100 ) );
と言った具合に使います。
function count_if_int(st:tstringlist;s:string;set_no:integer):integer;
var
i,i1,i2:integer;
st1:TStringList;
s1:string;
function choice_if:string;
begin
choice_if := '';
if s = '>' then begin
if set_no > i2 then begin
choice_if := st[i];
end;
end else if s = '>=' then begin
if set_no >= i2 then begin
choice_if := st[i];
end;
end else if s = '<' then begin
if set_no < i2 then begin
choice_if := st[i];
end;
end else if s = '<=' then begin
if set_no <= i2 then begin
choice_if := st[i];
end;
end else if s = '=' then begin
if set_no = i2 then begin
choice_if := st[i];
end;
end else if s = '<>' then begin
if set_no <> i2 then begin
choice_if := st[i];
end;
end else begin
//
end;
end;
begin
st1 := TStringList.Create;
st1.Clear;
for i := 0 to st.Count -1 do begin
i2 := strtoint(st[i]);
if '' <> choice_if then
st1.Add(s1)
end;
count_if_int := st1.Count;
st1.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
st:TStringList;
i:integer;
begin
st := TStringList.Create;
st.Text:= memo1.Text;
i := count_if_int(st,'=',8);
showmessage(inttostr(i));
st.Free;
end;
ツイート | ![]() |