Solved

Wildcard Function...

Posted on 2004-08-31
7
476 Views
Last Modified: 2010-04-05
Hello,
I need a wildcard function.

This is what it needs to beable to do.

Wildcard('MyString', '*String') = True
Wildcard('MyString', '*Str*') = True
Wildcard('MyString', 'My*') = True
Wildcard('MyString', 'Str*') = False

Thanks.
0
Comment
Question by:GreenNewt
7 Comments
 
LVL 4

Expert Comment

by:tobjectpascal
ID: 11941989
I was always led to believe that this was a fourm for people who got stuck because they could not get their code to work or needed a starting point, not for someone to say i need a function to do "This" or "That" i do believe www.rentacoder.com does that or am i totally mistaken?

Have you attempted to write this routine yourself? after all experts-exchange would imply that you also help others if and when you can.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11942067
I believe Delphi 7 has a 'like' function somewhere in the strings unit, which does something similar. Am not using Delphi 7 right now so I'm not sure. I know more in 8 hours, I hope, when I'm back home and behind my computer. :)
0
 

Author Comment

by:GreenNewt
ID: 11942106
Ok, Thanks for reply.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Accepted Solution

by:
Madshi earned 250 total points
ID: 11942165
http://help.madshi.net/StringCompare.htm#StrMatch

This part of my collection ("madBasic") is freeware and comes with full sources.
0
 

Author Comment

by:GreenNewt
ID: 11942197
Thank you, I will take a look. ;)
0
 

Author Comment

by:GreenNewt
ID: 11942328
Thanks Madshi
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 11942397
unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    inpStr: TEdit;
    Label2: TLabel;
    inpFind: TEdit;
    btnCheck: TButton;
    procedure btnCheckClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses StrUtils;

function Wildcard(Str, FindStr: string): boolean;
type TFindOption = (foStartsWith, foEndsWith, foContains);
var FindOption: TFindOption;
begin
  //by default i.e. when str starts and ends with * or even when no * are used
  FindOption:= foContains;

  //remove leading and trailing spaces
  Trim(Str);
  Trim(FindStr);

  if FindStr[1] = '*' then FindOption:= foEndsWith;
  if FindStr[Length(FindStr)] = '*' then
    if FindOption <> foEndsWith
      then FindOption:= foStartsWith
      else FindOption:= foContains;

  FindStr:= StringReplace(FindStr,'*','',[rfReplaceAll]);

  case FindOption of
    foStartsWith: result:= (FindStr = LeftStr(Str,Length(FindStr)));
    foEndsWith  : result:= (FindStr = RightStr(Str,Length(FindStr)));
    foContains  : result:= (Pos(FindStr,Str)<> 0);
  end;
end;

procedure TForm1.btnCheckClick(Sender: TObject);
begin
  if WildCard(inpStr.Text, inpFind.Text)
    then ShowMessage('Found')
    else  ShowMessage('Not Found');
end;

end.
~~~~~~~~
DFM File
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 233
  Height = 166
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 16
    Top = 16
    Width = 27
    Height = 13
    Caption = 'String'
  end
  object Label2: TLabel
    Left = 16
    Top = 48
    Width = 20
    Height = 13
    Caption = 'Find'
  end
  object inpStr: TEdit
    Left = 72
    Top = 16
    Width = 121
    Height = 21
    TabOrder = 0
  end
  object inpFind: TEdit
    Left = 72
    Top = 48
    Width = 121
    Height = 21
    TabOrder = 1
  end
  object btnCheck: TButton
    Left = 72
    Top = 88
    Width = 75
    Height = 25
    Caption = 'Check'
    TabOrder = 2
    OnClick = btnCheckClick
  end
end
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question