枚举和字符串互转
来源:互联网 发布:java 短信防刷 编辑:程序博客网 时间:2024/06/05 09:01
{ And you want in run-time to get a string with same value for each of them (for example, fill the Listbox items with enum values), then you can use the next procedure: }unit Unit1;interfaceuses TypInfo, Winapi.Windows, Winapi.Messages, System.SysUtils;type TMyEnum = (One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten); TMySet = set of TMyEnum; TTest = (test1, test2, test3); TTests = set of TTest;implementation//枚举转字符串数组procedure EnumToArs;var i: Integer; Ars: Array of String;begin for i := Ord(Low(TMyEnum)) to Ord(High(TMyEnum)) do begin SetLength(Ars, Length(Ars) + 1); Ars[high(Ars)] := (GetEnumName(TypeInfo(TMyEnum), i)); end;end;//字符串转枚举值function StrToEnum(const Value: String): TMyEnum;begin result := TMyEnum(GetEnumValue(TypeInfo(TMyEnum), Value));end;function EnumToStr(const Value: TMyEnum): String;begin result := GetEnumName(TypeInfo(TMyEnum), Ord(Value));end;function GetSetString(P: PTypeInfo; const Value): string;var i: Integer; BaseType: PTypeInfo;begin result := ''; BaseType := GetTypeData(P)^.CompType^; for i := 0 to High(Byte) - 1 do if i in TIntegerSet(Value) then result := result + GetEnumName(BaseType, i) + ','; if result <> '' then Delete(result, Length(result), 2); result := Format('[%s]', [result]);end;procedure GetSetValue(SetType: PTypeInfo; const Value: string; var result);var P, S: PChar; Len: Integer; EnumName: string; EnumType: PTypeInfo; procedure IncludeResult; begin Len := P - S; SetLength(EnumName, Len); Move(S^, EnumName[1], Len); EnumName := Trim(EnumName); Include(TIntegerSet(result), GetEnumValue(EnumType, EnumName)); end;begin TIntegerSet(result) := []; EnumType := GetTypeData(SetType)^.CompType^; P := PChar(Value); S := P; while True do case P^ of '[': begin Inc(P); S := P; end; ',': begin IncludeResult; Inc(P); S := P; end; #0, ']': begin IncludeResult; break; end; else Inc(P); end;end;function M1: String;begin result := GetSetString(TypeInfo(TTests), [test1, test2]);end;function M2: String;var V: TTests;begin GetSetValue(TypeInfo(TTests), '[test1, test2, test3]', V); if test1 in V then result := 'test1 in V'; if test2 in V then result := ('test2 in V'); if test3 in V then result := ('test3 in V');end;end.
0 0
- 枚举和字符串互转
- Objective-C 枚举类型和字符串互转方案
- Objective-C 枚举类型和字符串互转方案
- 枚举和字符串的转化
- 字符串转枚举类型
- 字符串转枚举
- 字符串转枚举
- 字符串转枚举类型
- 字符串转枚举
- 字符串转枚举(两个枚举类型)
- 根据枚举值取得描述和枚举定义字符串
- 字符串转换为枚举和整数转换为枚举
- 枚举和字符串的相互转化
- kotlin学习day10: 枚举和字符串模板
- 枚举值转换(字符串转换为枚举和整数转换为枚举)
- 枚举值转换(字符串转换为枚举和整数转换为枚举)
- 枚举值转换(字符串转换为枚举和整数转换为枚举)
- 枚举值转换(字符串转换为枚举和整数转换为枚举)
- Http Basic Authorizaition验证
- Objective-C iOS开发中常见的语句@synthesize obj=_obj的意义详解
- ajax
- 对话框程序菜单项可以禁用但不变灰问题的解决方法
- java中,如何安全的结束一个正在运行的线程?
- 枚举和字符串互转
- Hello !
- Linux下MySQL常用操作网络汇总总结
- 【servlet】servlet 笔记
- java线程并发
- 005_004 Python 统计值 并按照次数排序 可以通过字典的值对key进行排序
- 10种软件滤波方法的示例程序
- VC对话框全屏显示及相应控件位置改变
- apk