Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
how to use Case structure with strings Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
21-Jun-04
Category
Algorithm
Language
Delphi 3.x
Views
288
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: William Egge

I am sure every one at some time has wanted to use a case structure with strings 
but that is not possible with delphi case structures. So this is my solution which 
I have used over and over...

Answer:

I created a function called string index that returns the index of a string within 
an array of strings.  The function returns -1 if the string was not found and is 
not case sensitive. 

Usage: 

Simply use like this 
1   
2   case StringIndex('Edit', ['Post', 'Edit', 'Cancel']) of
3     0: ; // Do somthing for Post "command"
4     1: ; // Do somthing for Edit "command"
5     2: ; // Do somthing for Cancel "command"
6   end;
7   {. }
8   {. }
9   {. }
10  or
11  
12  if StringIndex('Edit', ['a', 'ab', 'rth']) = -1 then
13    // something
14  else
15    // something

     
StringIndex returns -1 if the string does not exist. This function is not case 
sensitive. 

Of course the first string 'Edit' would be some variable. 

Happy Coding! 

Source: 

16  unit Fn_StringIndex;
17  
18  interface
19  uses
20    SysUtils;
21  
22  function StringIndex(const SearchString: string; StrList: array of string): Integer;
23  
24  implementation
25  
26  function StringIndex(const SearchString: string; StrList: array of string): Integer;
27  var
28    I: Integer;
29  begin
30    Result := -1;
31    for I := 0 to High(StrList) do
32      if CompareText(SearchString, StrList[I]) = 0 then
33      begin
34        Result := I;
35        Break;
36      end;
37  end;
38  
39  end.


SOME EXAMPLES OF USAGE 

I decided it would be good for me to show all the places where I have used this 
function, so it is clearer the purpose I made it. 

These are cuts directly from live code. 

I hope this helps and maybe spark some ideas. 

In this case I used it to determine my action if the OrdProp was Boolean or 
WordBool 

40  EnumVal := GetOrdProp(Instance, PropInfo);
41  if StringIndex(PropInfo^.PropType^^.Name, ['Boolean', 'WordBool']) <> -1 then
42  begin
43    { ... }
44    // to much to explain here
45    { ... }
46  end
47  else
48    { ... }


Here I am determining what kind of text to return based on the float type name 
49  
50  tkFloat: case StringIndex(Info.PropInfo^.PropType^^.Name,
51    ['Currency', 'TDateTime']) of
52    0:
53      begin
54        Result := CurrToStr(GetFloatProp(Instance, PropInfo));
55      end;
56    1: Result := DateTimeToStr(GetFloatProp(Instance, PropInfo));
57  else
58    Result := FloatToStr(GetFloatProp(Instance, PropInfo));
59  end;
60  
61  //This converts a text string S to a boolean value. 
62  
63  function TRTTIModifier.TextToBool(S: string): Boolean;
64  begin
65    Result := StringIndex(S, ['', 'F', 'False', '0', 'No']) = -1;
66  end;


Taken from a web server application, to replace the passed tag "TagString" with 
whatever its suppose to.  This I think would be a must for anyone using borlands 
web server architech. 
67  
68  procedure TSite.StateHTMLTag(Sender: TObject; Tag: TTag;
69    const TagString: string; TagParams: TStrings; var ReplaceText: string);
70  begin
71    case StringIndex(TagString,
72      ['UserID', 'UserCreationDate', 'UserSiteAccessTime', 'APPCreationDate',
73      'UserList', 'Host'])
74      of
75      0: ReplaceText := IntToStr(User.ID);
76      1: ReplaceText := DateTimeToStr(User.CreationDateTime);
77      2: ReplaceText := DateTimeToStr(User.LastUserAccess);
78      3: ReplaceText := DateTimeToStr(App.CreationDateTime) + ' RefCount=' +
79        IntToStr(App.ReferenceCount);
80      4: ReplaceText := 'User List has been disabled';
81      5: ReplaceText := App.Host;
82    end;
83  end;


Another snip from a web server app where the command was passed on the url. I am 
only giving a small snip of it, but I think you get the idea. 

84  else
85    case StringIndex(Command,
86      ['EDITOBJECT', 'MODIFY', 'GENFORM', 'DELETE',
87      'PREVIEW', 'EDIT', 'CREATESITE', 'Reload',
88        'CreateSubSite', 'LogOut', 'HistoryBack', 'HistoryForward',
89        'PublishSite', 'Copy']) of
90      0:
91        begin
92          Response.Content := EditObject(PassedObject, SubCommand);
93        end;
94      1:
95        begin


This is taken from a some laser controler software I wrote.... You can see that I 
am determining what method to call based on the file extension. 

96  procedure TBoneEditorData.LoadAnyFile(const FileName: string);
97  var
98    Ext: string;
99  begin
100   Ext := ExtractFileExt(FileName);
101   case StringIndex(Ext, ['.fmc', '.ild', '.fbz', '.fgp', '.fif']) of
102     0: LoadMotionFile(FileName);
103     1: LoadIldaFile(FileName);
104     2: LoadBonesFile(FileName);
105     3: LoadCombo(FileName);
106     4: LoadImageFile(FileName);
107   else
108     raise Exception.CreateFmt('Cannot load files of this type extension "%s"', 
109 [Ext]);
110   end;
111 end;


This takes a code from a database field and converts it to an ord type 

112 function TMDBSchedule.GetFrequency: TFrequency;
113 const
114   FreqArray: array[0..4] of string = ('Y', 'M', 'W', 'D', 'N');
115 var
116   FreqInput: string;
117   FreqOrd: integer;
118 
119 begin
120   FreqInput := UpperCase(DataSet.FieldByName('Frequency').AsString);
121 
122   FreqOrd := StringIndex(FreqInput, FreqArray);
123 
124   case FreqOrd of
125     0: Result := frYearly;
126     1: Result := frMonthly;
127     2: Result := frWeekly;
128     3: Result := frDaily;
129     4: Result := frNone;
130   else
131     Result := frNone;
132   end;
133 end;


Thats all the most important stuff.... have fun! 


Component Download: http://www.baltsoft.com/files/dkb/attachment/fn_stringindex.zip

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC