Bass Bass - 3 months ago 31
C# Question

Matching string with wildcard character

I've the following code:

string d = "OR.ISSUE226568";
string p;
switch (d)
{
case "VOID":
p = "VOID";
break;

case "OR.*":
p = "Reissue";
break;
}


The problem is that right now the second case is not working.

I need something that can act as a wildcard character so that any "OR.ISSUE1234567" could be identified and the proper value assigned to the p variable.

So if d begins with "OR." the value of p will be "Reissue".

Any ideas on how to do that?

Answer

You can do this by either switch to using a conditional instead of a switch statement:

if (d == "VOID") 
{
    p = "VOID";
}
else if (d.StartsWith("OR."))
{
    p = "Reissue";
}

Or you could look at using a Regex to match your string if you want to do something more complicated with it (see MSDN).