Jump to content
  • Advertisement
Sign in to follow this  

[.net] String comparison using wildcards.

This topic is 4383 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to sarch substrings of a string using wildcards, for example if i wanted to find out if a string contained "santa*. Rides" ?

Share this post


Link to post
Share on other sites
Advertisement
The quicker but also much more powerful way is to use RegEx - it's built in to the .NET framework and should have everything you need.
Try these articles, but feel free to ask for help with explanations, etc.
http://www.radsoftware.com.au/articles/regexsyntaxadvanced.aspx
http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx

Hope it helps. :)

Share this post


Link to post
Share on other sites
Answer to post #1:
Yeah i found that earlier but didn't like it so i wrote my own. This is for searching inside my own little database that i'm writing for a program. Might not be the most optimised but it's simple.


public File[] Search(string searchPattern, bool ignoreCase, DatabaseSearchOptions searchOptions)
{
List<File> findList = new List<File>();

// We'll split up the searchPattern using the wildcards.
string[] wildcards = { "*", "?" };

// Check the logic.
if (ignoreCase)
searchPattern = searchPattern.ToLower();

// Split up the search pattern using the wildcards.
string[] comparers = searchPattern.Split(wildcards, StringSplitOptions.RemoveEmptyEntries);

// We need it for the search logic.
bool found;
string checkAgainst;
foreach (File file in fileList)
{
found = true;

// Having a switch inside a loop isn't something that's recommended performance wise.
switch (searchOptions)
{
case DatabaseSearchOptions.NameOnly:
checkAgainst = file.Name;
break;

case DatabaseSearchOptions.PathOnly:
checkAgainst = file.Path;
break;

case DatabaseSearchOptions.NameAndPath:
checkAgainst = file.Path + "\\" + file.Name;
break;

default:
checkAgainst = file.Name;
break;
}

if (ignoreCase)
checkAgainst = checkAgainst.ToLower();

// Compare the string against the comparers.
foreach (string c in comparers)
{
// No occurence found.
if (checkAgainst.IndexOf(c) == -1)
{
found = false;
break;
}
}

// We found a match.
if (found)
findList.Add(file);
}

return findList.ToArray();
}




Post(s) 1+:

Thanks, i'll look into it :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!