Click here to Skip to main content
13,354,885 members (86,991 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 18 May 2005

Wildcard Manipulation for Text

, 18 May 2005
Rate this:
Please Sign up or sign in to vote.
Searches and replaces text based on a pattern that uses wildcards.

Sample Image - diskinfo.png


Sometimes it is handy to have an utility that will search for a certain text pattern or execute a find and replace based on it. The string provides some limited ability to do that, but you are out of luck if you need these functions to be case insensitive and to use a wildcard algorithm.


The tradeoff for this situation is that, you cannot look for ‘*’ and ‘?’ in your target text. But that’s what the string is good for, isn’t it!

I’ve included a solution with two projects. The DLL has the code that implements algorithm and the executable is merely a client that would let you enjoy the functionality in a visual way. Here is the code that's the heart for this algorithm.

protected bool _PatternMatched(int IndexText, int IndexPattern)
  while (true)
    char CrtTxtChar, CrtPatternChar;
      (IndexPattern == patternlen)?'*':ConvertCase(strpattern[IndexPattern++]);
    switch (CrtPatternChar )
      case '*':
        if(IndexPattern == patternlen)//last *, used by FindPatternPosition
          this.last = IndexText;
          return true;// last pattern char == *, no need to check the last chars
        while (IndexText < txtlen)
            return true;
        return false;
      case '?':
        if(IndexText == txtlen)
          return false;
        if(IndexText == txtlen)
          CrtTxtChar = '\0';
          CrtTxtChar = ConvertCase( strtxt[IndexText++] );
        if( CrtTxtChar != CrtPatternChar )  // check for exact char
            return false;
          else if(_PatternMatched(IndexText, 0))
            if(this.first < IndexText)
              this.first = IndexText;
            return true;
            return false;
          if(this.last < IndexText -1)
            this.last = IndexText -1;
    }//switch ends
  }//while ends
}//_PatternMatched ends

This protected method is used by the public methods IsPatternMatch and ReplaceTextEx that are exposed by this class. The first and last data members store the position of the pattern in the target string in order to allow the replace to work.

Using the code

Just instantiate a class of WildCardTxtUtility and call its public functions.


A large target text and multiple wildcards in the pattern will cause performance issues. I would welcome any attempt to address them.


  • This is version 1.0.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer (Senior)
United States United States
Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.

You may also be interested in...

Comments and Discussions

Generalbug report Pin
Tiger.xing8-Dec-05 19:42
memberTiger.xing8-Dec-05 19:42 
GeneralRe: bug report Pin
dmihailescu7-Feb-07 6:30
memberdmihailescu7-Feb-07 6:30 
Generalanother bug fix Pin
dmihailescu8-Feb-07 5:21
memberdmihailescu8-Feb-07 5:21 
GeneralRegular Expressions Pin
Eric Woodruff18-May-05 17:22
memberEric Woodruff18-May-05 17:22 
GeneralRe: Regular Expressions Pin
Uwe Keim18-May-05 19:39
sitebuilderUwe Keim18-May-05 19:39 
GeneralI did not know about them Pin
dmihailescu19-May-05 3:57
memberdmihailescu19-May-05 3:57 
Sigh | :sigh: I honestly did not know about these features.
However, it seems that they were not part of the .net framework 1.0, and they've shown up only in 1.1.
If somebody uses that old version rather than 1.1, than I hope my article will help. Sniff | :^)
GeneralRe: Regular Expressions Pin
zwir31-Jul-07 23:40
memberzwir31-Jul-07 23:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 18 May 2005
Article Copyright 2005 by dmihailescu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid