Statement: This article is original by Xiong Heng (Beta). If you want to reprint it, please keep itArticleComplete.
Use the string --- by Xiong Heng (Beta) in the case statement)
What I want to introduce today is an alternative method. As we all know, the case statement can only be of the sequence type
(Ordinal type), so let's take a look at what sequence types have: 1) integer; 2) Word
Character type; 3) Enumeration type.
So there are three methods to use strings in the case statement.
-P ):
Suppose our specific application is as follows (ideal, but incorrect syntax ):
STR: = 'chungqing ';
Case str of // the compiler will stop you in this line
'Beijing': showmessage ('first ');
'Tiancjing': showmessage ('second ');
'Shanghai': showmessage ('third ');
'Chungqing': showmessage ('forth ');
Else showmessage ('other ');
End;
Method 1: Convert string to integer type
This should be a relatively common method, mainly using tstringlist. First, separate the alternative strings.
Add, and then call its indexof method. This method returns an integer indicating that the string to be searched is output.
Position in the list:
// Var strlist: tstringlist;
Strlist: = tstringlist. Create;
Strlist. Add ('beijing ');
Strlist. Add ('tianjing ');
Strlist. Add ('shanghai ');
Strlist. Add ('chongqing ');
STR: = 'chungqing ';
Case strlist. indexof (STR)
0: showmessage ('first ');
1: showmessage ('second ');
2: showmessage ('third ');
3: showmessage ('forth ');
Else showmessage ('other ');
End;
Strlist. Free;
As you can see, this is a troublesome method, but you have to remember this method because it will be discussed later.
This is the most common method.
Also: Of course, converting a string to an integer is not just a case method. Another method is
All the backup options are converted to an equal length and then merged into a string. Then, the POs function is used to return
Location. Please refer 《ProgramMember magazine (which issue do not remember :-().
Method 2: Convert string to character type
This should be the simplest method, but the limitations are relatively large. If the nth word of your backup Option
Operators (N should be constants) are not the same, so you have earned. In this way, you can retrieve this character,
To uniquely identify your string:
STR: = 'chungqing ';
Case STR [1] of // the first character is different, so retrieve the first character for comparison.
'B': showmessage ('first ');
'T': showmessage ('second ');
'S ': showmessage ('third ');
'C': showmessage ('forth ');
Else showmessage ('other ');
End;
However, if your backup option does not have this feature, you will not be able to use this method.
Method 3: Convert string to Enumeration type
This method mainly uses the features of rtti. The main idea of this method is to first name all the slave options.
For an enumerated value, you only need to convert the string to the enumerated type.
Use the case statement. So how can I convert a string to an enumeration type? Look down:
// Uses typinfo; // remember to reference this unit
// Type tmystrsel = (Beijing, Tianjing, Shanghai, Chongqing );
// Note that the above definition cannot be placed inside a function. In that case, it will have no running class information.
// Var strsel: tmystrsel;
STR: = 'chungqing ';
Strsel: = tmystrsel (getenumvalue (typeinfo (tmystrsel), STR ));
Case strsel
Beijing: showmessage ('first ');
Tianjing: showmessage ('second ');
Shanghai: showmessage ('third ');
Chongqing: showmessage ('forth ');
Else showmessage ('other ');
End;
The getenumvalue function returns the enumerated value corresponding to a string.
(You must know that the enumeration type is ordered, or how to call the sequence type?), and then pass
A forced type conversion converts this value to an enumeration type. So we can convert the string to the enumerated type.
Operation.
We can see that in the case statement section, our use is almost the same as the ideal use method!
After all, the enumeration type can be known. It looks more intuitive than using the first two methods.
Of course, this is not a omnipotent method. If your standby option has a variable name that does not conform to Delphi
The naming rules (such as 'AK-47 'or Chinese) cannot be declared as an enumeration type, so this
Methods. If so, you can only use the first two methods. In extreme cases, at least you have the first
But in many cases, this method is applicable.
Http://www.delphibbs.com/delphibbs/dispq.asp? Lid = 1358621
Continue to read the full text of "the second article of Beta: Using strings in case statements...