在 case 语句中使用字符串-转
http://www.cnblogs.com/del/archive/2008/07/08/1237856.html
非常遗憾 Delphi 的 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也不错;一般用枚举;
在 case 语句中使用字符串-转的更多相关文章
- switch case语句中能否作用在String,long上
在之前的eclipse中使用switch的case语句时是只能为(byte,short,char)int类型或枚举类型.但在jdk1.7以后 在case语句中是可以使用String 以及long 等类 ...
- Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项)
Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项) A.cat B.concat C.join D.+ 解答:B
- 在switch中的case语句中声明变量编译出错的解决方案
在switch中的case语句中声明变量编译的问题 先来看段代码,别管什么意思: : , j = ; ; i < ; i++) recive_phone[i] = msgbuf.text[i]; ...
- 在switch中的case语句中声明变量会被提前
原文链接:http://my.oschina.net/u/2000201/blog/514384 本人今天在编写工具类时,无意之间发现,在Java的Swith语句的case语句中声明局部变量时出现了一 ...
- swtich和case语句中,定义变量要加花括号
转自: http://blog.chinaunix.net/uid-27103408-id-3340702.html http://www.xuebuyuan.com/2070170.html swi ...
- 关于Mysql select语句中拼接字符串的记录
在mysql的SELECT语句中拼接两列(或多列)的字符串显示: mysql> select concat(dname,loc) from dept; 以上语句便把dept表的dname,loc ...
- sql语句中截取字符串
今天在开发过程中因为要用到合并单元格,在程序里实现了以后,查出来的数据太长,都把格式撑大了,后来想想可以在sql语句查询的时候就截取,就去网上找了一下,挺好用,就转了过来: 合并单元格: /// &l ...
- [hive]case 语句中字符串匹配
当使用case when时,有时会需要对某个字段做子串匹配.如果是在where条件中,我们会直接使用 like '%xx%'来匹配,但case when语句不行 这时需要使用instr函数 examp ...
- 【特性】select语句中使用字符串链接获取字段值失败
坑1 在一个多行的表中,想把其中的一个字段值拿出来,组成一个字符串供后面使用. 按照以往,自己就如以下这么写了: declare @sql varchar(8000) set @sql='insert ...
随机推荐
- 编译安装PHP7并安装Redis扩展Swoole扩展
编译安装PHP7并安装Redis扩展Swoole扩展 在编译php7的机器上已经有编译安装过php5.3以上的版本,从而依赖库都有了 本php7是编译成fpm-php 使用的, 如果是apache那么 ...
- libvirt 网络手册(一)
如果选择网络类型 在一个专用的服务器上,虚拟机常常需要被从公网访问(也就是每个虚拟机都需要公网地址),这时就需要桥接网络,它使得每个虚拟机有自己的IPV4和IPV6地址.如果桥接不可用,可以创建一个R ...
- Linux下的压缩和解压缩命令——tar
tar命令 选项: -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只 ...
- Rails 5 开发进阶
Rails 5 开发进阶:https://www.gitbook.com/book/kelby/rails-beginner-s-guide/details cancan : http://blo ...
- APM程序分析-AC_WPNav.cpp
APM程序分析 主程序在ArduCopter.cpp的loop()函数. /// advance_wp_target_along_track - move target location along ...
- android 对话框 setMultiChoiceItems 设置 初始化勾选
只需要 设定第二个参数 boolean[] 值就好了
- iOS内支付
- PermGen space
MyEclipse内存空间不足,调整空间操作: 1.点击"Run"-"Run Configurations",在打开的窗口中点击"Arguments& ...
- 《learning hard C#学习笔记》读书笔记(19)多线程
19.1 多线程编程知识 19.1.1 进程与线程的概念 进程: 可以理解为一块包含某些资源的内存区域,操作系统通过进程方式把它工作划分为不同的单元. 一个应用程序可以对应多个进程. 线程: 线程是进 ...
- ubuntu selinux
apt install selinux-utils apt install policycoreutils https://zhidao.baidu.com/question/917938889387 ...