I thought we had the capability to compare one string against multiple values, using a case statement. I can't seem to make it work though, so I've produced the following routine. Hoping someone can tell me an official way of doing this, but sharing the code in case there isn't.Code: Pascal [Select][+][-]Function IsIn(sSearch: String; Const AValues: Array Of Const): Boolean;Var i: Integer; Begin Result := False; For i := Low(AValues) To High(AValues) Do Begin If (AValues[i].vType = vtAnsiString) Then Result := Ansistring(AValues[i].vAnsiString) = sSearch; If Result Then Break; End;End; Example of use.Code: Pascal [Select][+][-] sExt := Lowercase(ExtractFileExt(sFilename)); If IsIn(sExt, ['.pkt', '.mpg', '.mp4', '.mkv', '.avi', '.wmv', '.asf']) Then Begin
if Pos('.asf', '.pkt.mpg.mp4.mkv.avi.wmv.asf') > 0 then ShowMessage('Supported');
Quote from: wadman on October 19, 2018, 07:18:29 amif Pos('.asf', '.pkt.mpg.mp4.mkv.avi.wmv.asf') > 0 then ShowMessage('Supported'); That's genius, and yes - would have worked perfectly for me :-)
I wanted to mention that wadman's solution using Pos is likely more efficient (faster) than using a case statement and generates less code too, not to mention that it is easier to see at a glance which extensions are supported since they are all on one line instead of spread around many. Using Pos in that case is simple, concise and elegant.
Quote from: 440bx on October 19, 2018, 07:44:41 amI wanted to mention that wadman's solution using Pos is likely more efficient (faster) than using a case statement and generates less code too, not to mention that it is easier to see at a glance which extensions are supported since they are all on one line instead of spread around many. Using Pos in that case is simple, concise and elegant.Yeah and it gives false positives as well e.g. this will evaluate to being > 0: Code: Pascal [Select][+][-] Pos('.mp', '.pkt.mpg.mp4.mkv.avi.wmv.asf') Bart
Yeah and it gives false positives as well e.g. this will evaluate to being > 0:
var ext: string;begin ext := '.mp'; if Pos(ext+'.', '.pkt.mpg.mp4.mkv.avi.wmv.asf.') > 0 then ShowMessage('Supported');end;
Add a period to both strings