Delphi在case语句中使用字符串


本文整理自网络,侵删。

 
在case语句中使用字符串   

在 case 语句中使用字符串 --- by 熊恒(beta)

我今天要介绍的是一个比较另类的方法。大家都知道,case 语句只能对顺序类型
            (ordinal type)管用,那么我们先看一下顺序类型到底有那些呢:1)整型;2)字
            符型;3)枚举型。
            于是也就演化出三种在 case 语句里面使用字符串的方法(耐心看下去哦,最后一
            种方法才是今天的重点:-p):

假如我们的具体应用如下(理想的,但是错误的写法):

str := 'Chongqing';
            case str of // 编译器将在这一行制止你 :-(
            'Beijing': ShowMessage('First');
            'Tianjing': ShowMessage('Second');
            'Shanghai': ShowMessage('Third');
            'Chongqing': ShowMessage('Forth');
            else ShowMessage('Other');
            end;

(**** 转载敬请注明-本文出处:南山古桃(nsgtao) ****)
法一:字符串转为整型
            这应该是比较通行的方法了,主要是利用 TStringList。先把备选的字符串挨个
Add 进去,然后调用其 IndexOf 方法。该方法返回一个整数,表示待找字符串出
            现在列表中的位置:

//var strList: TStringList;

strList := TStringList.Create;
            strList.Add('Beijing');
            strList.Add('Tianjing');
            strList.Add('Shanghai');
            strList.Add('Chongqing');
            str := 'Chongqing';
            case strList.IndexOf(str) of
            0: ShowMessage('First');
            1: ShowMessage('Second');
            2: ShowMessage('Third');
            3: ShowMessage('Forth');
            else ShowMessage('Other');
            end;
            strList.Free;

可以看到,这是比较麻烦的方法了,不过你可得记住这个方法,因为后面的讨论
            将会讲到,这是最通用的方法。
            另:当然,把字符串转化为整型后用于 case 不只这一种方法。另一种是通过把
            备选项全部转化为等长度后合并为一个字符串,然后用 Pos 函数返回某字符串的
            位置。在此不再累述,请查阅《程序员》杂志(具体哪一期不记得了:-()。

(**** 转载敬请注明-本文出处:南山古桃(nsgtao) ****)
法二:字符串转为字符型

这应该是最简单的一种方法,不过局限性比较大。如果你的备选项的第 N 个字
            符(N 应为常数)都互不相同,那么你赚到了。这样,就可以通过取出这个字符,
            来唯一标识你的字符串:

str := 'Chongqing';
            case str[1] of // 第一个字符都不同,所以取出第一个进行比较
'B': ShowMessage('First');
            'T': ShowMessage('Second');
            'S': ShowMessage('Third');
            'C': ShowMessage('Forth');
            else ShowMessage('Other');
            end;

不过如果你的备选项没有这样的特性,那么你就无缘使用这种方法了。


法三:字符串转为枚举型

这种方法主要用到 RTTI 的特性。该方法的主要思路是,先把所有的备选项声名
            为一个枚举类型的值,那么我们只要把要找的字符串也转换为枚举型,那么就可以
            用 case 语句了。那么通过什么办法把一个字符串转化为枚举型呢?往下看:

// uses TypInfo; // 记得引用这个单元
// type TMyStrSel = (Beijing, Tianjing, Shanghai, Chongqing);
            // 注意,上面这个定义不能放在某个函数内部哦,那样的话,它就没有运行类信息了
// var strSel: TMyStrSel;

str := 'Chongqing';
            strSel := TMyStrSel(GetEnumValue(TypeInfo(TMyStrSel), str));
            case strSel of
            Beijing: ShowMessage('First');
            Tianjing: ShowMessage('Second');
            Shanghai: ShowMessage('Third');
            Chongqing: ShowMessage('Forth');
            else ShowMessage('Other');
            end;

稍做解释:GetEnumValue 函数返回一个字符串对应的枚举型的值在某枚举类型
            中的位置(要知道枚举类型是有顺序的,要不怎么叫顺序类型呢:-)),然后通过
            一个强制类型转换将这个值转化为枚举型。于是就实现了把字符串转化为枚举型的
            操作。

可以看到,在 case 语句这一段,我们的使用和理想中的使用方法几乎是一样的!
            毕竟枚举类型可以做到见名知意嘛。的确比用前两种方法看起来直观得多。

当然,这并不是万能的方法,如果你的备选项有一个不符合 Delphi 的变量名命
            名法则(如'AK-47'或中文等),则不能将其声明为一个枚举型,于是就不能使用这
            个方法。如果是这样你就只能使用前两种方法了。极端的情况下,至少你还有第一
            种方法可以使用 :-) 但在不少的场合,这种方法还是适用的。


Case 语句中使用字符串
            作者:

function CaseString (const s: string;
                                  const x: array of string): Integer;
            var i: Integer;
            begin
            Result:= -1; // Default return parameter
            for i:= Low (x) to High (x) do begin
            if s = x[i] then begin Result:= i; Exit; 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;
            /////////////////////////////////

const MatchingStrings = '*First*Second*Third*';
            var sString: string;
            ...

// sString has the data you want to test
            case pos('*'+sString+'*',MatchingStrings) of
                1:     // This is the match for 'First'
                7:     // This is the match for 'Second'
                14:    // This is the match for 'Third'
                else // In this case there were no matches
                end;

unit Unit1;

interface

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

type
              TForm1 = class(TForm)
                 btn1: TButton;
                 grp1: TGroupBox;
                 rb1: TRadioButton;
                 rb2: TRadioButton;
                 procedure btn1Click(Sender: TObject);
              private
                 { Private declarations }
              public
                 { Public declarations }
              end;

type TASK=(blue,green,black,white,maroon);

var
              Form1: TForm1;

const
                Taskzhaowang:TASK=blue;
                Taskneizheng:TASK=green;

implementation

{$R *.dfm}

procedure asdf(t:task)  ;
            begin
              case t of
                  blue:
                     ShowMessage('ZhaoWang:'+inttostr(Ord(t)));
                  green:
                     ShowMessage('NeiZhneg:'+inttostr(Ord(t)));
              end;
            end;

procedure TForm1.btn1Click(Sender: TObject);
            begin
              if(rb1.Checked) then asdf(Taskzhaowang);
              if(rb2.Checked) then asdf(Taskneizheng);
            end;

end.

相关阅读 >>

Delphi net.httpclient用最精简的代码获取网页数据

Delphi中write,read,writeln和readln的用法

Delphi比较两个位图是否相同

Delphi idhttp post 提交 json

Delphi tfilestream 的读写 合并文件

Delphi 获取指定年月的周、日数

Delphi 全盘搜索指定文件

Delphi xe5 安卓设置屏幕横屏竖屏

Delphi memo1删除前n行

embarcadero开源项目赞助

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...