C# if string match found boolean - streamlined code?

  • Thread starter Thread starter jason
  • Start date Start date
J

jason

Though this code appears to work, I suspect it could be streamline.
Total Noob Here.


public bool isit(string c1)
{
string color1 = "blue green red";
Regex re = new Regex("@"+c1, RegexOptions.IgnoreCase |
RegexOptions.Multiline);
if (re.Matches(color1).Count > 0)
{
return (true);
}
else
{
return (false);
}


==
I found a sample on the net that does this

if (re.color1 != 0 )

But I get an error about Regex not having a definition for color1

==
Also, what if color1 was an enum, how would test the search string can
be found in any of the items?
 
how about

switch(c1) {
case "blue":
case "green":
case "red":
return true;
default:
return false;
}

More generally, you could use
return re.IsMatch(color1);

If it was an enum, then the Enum helper class can do a lot (i.e. list
available items, parse items, etc); e.g.

enum MyColors { Blue, Red, Green };
static void Main(string[] args) {
Console.WriteLine(IsColor("blue"));
Console.WriteLine(IsColor("red"));
MyColors value;
if (TryMatchColor("green", out value)) {
Console.WriteLine(value);
}
if (TryMatchColor("turquoise", out value)) {
Console.WriteLine(value);
}
}
static bool IsColor(string name) {
MyColors color;
return TryMatchColor(name, out color);
}
static bool TryMatchColor(string name, out MyColors value) {
try {
value = (MyColors) Enum.Parse(typeof(MyColors), name, true);
return true;
} catch (ArgumentException) {
value = 0; // default
return false;
}
}
 
OK - reading again I may have missed your intention... forgetting about
solving it via Regex (or any other approach) - what exactly are you trying
to do? i.e. input {x}, output {y}...?

Marc
 
public bool isit(string c1)
{
string[] color1 = new string[] {"blue", "green", "red"};
return (Array.IndexOf<string>(color1, c1.ToLower()) > -1);
}

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
 
Marc said:
OK - reading again I may have missed your intention... forgetting about
solving it via Regex (or any other approach) - what exactly are you trying
to do? i.e. input {x}, output {y}...?

Marc

The post with the array is going to work.. I wanted to use Regex
because some of the entries will be sentences and I might need to
search for patterns. I know I just asked for a boolean, but Ideally I
wanted to return an array of every entry where the pattern was found.

Thank you!
 
Kevin said:
public bool isit(string c1)
{
string[] color1 = new string[] {"blue", "green", "red"};
return (Array.IndexOf<string>(color1, c1.ToLower()) > -1);
}


Thank you.. this is a great help.

if you have it handy.. what if the enteries were very long sentences
and It was a pattern I was searching for (I simplified my original
question). And what if I wanted to return an array of lines that had
the match. I know I might be pushing it and if so I'll go dig around -
I have a very similar VB code I'm trying to migrate over.

Thanks again - really appreciate it!
 
Back
Top