首页  编辑  

Case语句的串检索

Tags: /超级猛料/Language.Object Pascal/在Delphi编程中使用C语言代码/   Date Created:

case结构不支持了符串检索,怎么办

do like this:

const

  strArray : array [0..10] of string =(['str0', 'str1', 'str2', ...);

for i := 0 to 10 do

 if strArray[i] = str then

case i of

 0: do str1 thing

 1: do str1 thing

 2:

 ...

end;

*****************

function CaseString (const s: string; // Search for

                    ci: Boolean;

                    // if true looking case insensitive

                    p: Boolean;

                    // if true looking if s is in the compared string

                    const x: array of string): Integer;

var i: Integer;

   s1, s2: string;

begin

Result:= -1;

for i:= Low (x) to High (x) do begin

 s1:= s;  s2:= x[i];

 if ci then begin

  AnsiUpperCase(s1);

  AnsiUpperCase(s2);

 end;

 if p then begin

  if Pos (s1, s2) > 0 then begin  Result:= i;  Exit;  end;

 end else begin

  if s1 = s2 then begin  Result:= i;  Exit;  end;

 end;

end;

end;  

调用:

search:= 'delphi3000';

case CaseString (search, ['delphi3000',

                          'delphipages',

                          'Torry's']) of

 0: s:= 'Excellent!';

 1: s:= 'Good source';

 2: s:= 'Not bad!';

end;

***************************************************

uses

 TypInfo;

type

 TNumericChoiceParent = (ncp_Mother, ncp_Father, ncp_Child);

procedure TForm1.btChooseClick(Sender: TObject);

var

 S: string;

begin

 S := InputEdit.Text;

 case TNumericChoiceParent(GetEnumValue(TypeInfo(TNumericChoiceParent), 'ncp_' + S)) of

   ncp_Mother: ShowMessage('Hello Mom :o)');

   ncp_Father: ShowMessage('Hi, Dad -]');

   ncp_Child: ShowMessage('Shut up and eat your soup !-(');

 else

   ShowMessage('Who do you think that you are?');

 end;

end;