在 case 语句中使用字符串-转

http://www.cnblogs.com/del/archive/2008/07/08/1237856.html

非常遗憾 Delphi 的 case 语句不支持字符串, 但我觉得这也可能是基于效率的考量;
如果非要在 case 中使用字符串, 也不是不可以变通, 这里提供了五种方法.


本例效果图:

在 case 语句中使用字符串-转


代码文件:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls; type
  TForm1 = class(TForm)
    RadioGroup1: TRadioGroup;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  end; var
  Form1: TForm1; implementation {$R *.dfm} uses TypInfo; {操作枚举用} {初始化一个单选组, 用于测试}
procedure TForm1.FormCreate(Sender: TObject);
begin
  RadioGroup1.Items.CommaText := 'a,bb,ccc,dddd';
  RadioGroup1.ItemIndex := ;
end; {这是 case 语句比较常规的用法}
procedure TForm1.Button1Click(Sender: TObject);
begin
  case RadioGroup1.ItemIndex of
    : Color := clRed;
    : Color := clYellow;
    : Color := clLime;
    : Color := clBlue;
  end;
end; {方法一: 假如要 case 的字符串的长度不同}
procedure TForm1.Button2Click(Sender: TObject);
var
  str: string;
begin
  str := RadioGroup1.Items[RadioGroup1.ItemIndex];
  case Length(str) of
    : Color := clRed;
    : Color := clYellow;
    : Color := clLime;
    : Color := clBlue;
  end;
end; {方法二: 假如要 case 的字符串的第一个字母不同, case 是支持字符的}
procedure TForm1.Button3Click(Sender: TObject);
var
  str: string;
begin
  str := RadioGroup1.Items[RadioGroup1.ItemIndex];
  case str[] of
    'a': Color := clRed;
    'b': Color := clYellow;
    'c': Color := clLime;
    'd': Color := clBlue;
  end;
end; {方法三: 借用 TStringList}
procedure TForm1.Button4Click(Sender: TObject);
var
  List: TStringList;
  str: string;
begin
  List := TStringList.Create;
  List.Text := RadioGroup1.Items.Text;   str := RadioGroup1.Items[RadioGroup1.ItemIndex];
  case List.IndexOf(str) of
    : Color := clRed;
    : Color := clYellow;
    : Color := clLime;
    : Color := clBlue;
  end;   List.Free;
end; {方法四: 借用枚举}
type
  TMyEnum = (a, bb, ccc, dddd); procedure TForm1.Button5Click(Sender: TObject);
var
  MyEnum: TMyEnum;
  str: String;
begin
  str := RadioGroup1.Items[RadioGroup1.ItemIndex];   MyEnum := TMyEnum(GetEnumvalue(TypeInfo(TMyEnum), str));
  case MyEnum of
    a    : Color := clRed;
    bb  : Color := clYellow;
    ccc  : Color := clLime;
    dddd : Color := clBlue;
  end;
end; {方法五: 利用对比字符串返回的整数, 这种方法并不太可靠, 但在某种情况下会更灵活}
procedure TForm1.Button6Click(Sender: TObject);
var
  str: string;
begin
  str := RadioGroup1.Items[RadioGroup1.ItemIndex];
  case CompareStr(str, 'a') of
    : Color := clRed;
    : Color := clYellow;
    : Color := clLime;
    : Color := clBlue;
  end;
end; end.

窗体文件:

object Form1: TForm1
Left =
Top =
Caption = 'Form1'
ClientHeight =
ClientWidth =
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnCreate = FormCreate
PixelsPerInch =
TextHeight =
object Button1: TButton
Left =
Top =
Width =
Height =
Caption = 'Button1'
TabOrder =
OnClick = Button1Click
end
object Button2: TButton
Left =
Top =
Width =
Height =
Caption = 'Button2'
TabOrder =
OnClick = Button2Click
end
object Button3: TButton
Left =
Top =
Width =
Height =
Caption = 'Button3'
TabOrder =
OnClick = Button3Click
end
object RadioGroup1: TRadioGroup
Left =
Top =
Width =
Height =
Caption = 'RadioGroup1'
TabOrder =
end
object Button4: TButton
Left =
Top =
Width =
Height =
Caption = 'Button4'
TabOrder =
OnClick = Button4Click
end
object Button5: TButton
Left =
Top =
Width =
Height =
Caption = 'Button5'
TabOrder =
OnClick = Button5Click
end
object Button6: TButton
Left =
Top =
Width =
Height =
Caption = 'Button6'
TabOrder =
OnClick = Button6Click
end
end

本人用过Pos也不错;一般用枚举;

上一篇:Java OOP中的字符串篇


下一篇:Mikit前端框架,轻量级CSS&JS前端框架