Sign in to follow this  
Cybrosys

[.net] String comparison using wildcards.

Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this