xscape
xscape

Reputation: 3376

C#: String Manipulation WildCard Search

I am creating a search application which searches a collection from a given string + *

For example I have this string collection:

SMITH

SMATH

BATH

SMAG

x

Test

When the user input *TH the output should be SMITH, SMATH and BATH

When the user input SM*TH the output should be SMITH and SMATH

When the user input SM* the output should be SMITH, SMATH and SMAG

Do you have any suggestion on how to do this?

Upvotes: 0

Views: 3731

Answers (1)

Shaun Wilde
Shaun Wilde

Reputation: 8378

Have a look at regular expressions in .NET

If you only have a wildcard * I'd probably replace this with a .* (0 or more characters) or a .+ (1 or more characters) in the first instance

something like (not tested but should have all the elements to get you going)

var pattern = "SM*TH"; 
var newpattern = pattern.Replace("*",".+"); 
var rex = new RegEx(newpattern); 
var match = rex.Match("SMITH")

Upvotes: 1

Related Questions